➡️➡️Java (Handwritten) Notes
----
Java is a versatile and widely-used programming language that is object-oriented, platform-independent, and designed to have as few implementation dependencies as possible. Below are some essential notes and common interview questions that can help beginners and freshers get a strong grasp of Java and prepare for interviews.
1. Java Basics
1.1. Introduction to Java:
- Platform Independence: Java code is compiled into bytecode, which can run on any system that has the Java Virtual Machine (JVM), making it platform-independent.
- Object-Oriented: Java is based on the concepts of objects and classes.
- Simple and Secure: Java is designed to be easy to use and secure, with built-in garbage collection and exception handling.
1.2. Java Development Kit (JDK):
- JDK: Includes tools like the compiler (
javac
), interpreter (java
), and other utilities needed to develop Java applications. - JRE: The Java Runtime Environment (JRE) is a part of the JDK that provides the libraries, Java Virtual Machine (JVM), and other components to run applications written in Java.
1.3. Basic Syntax:
javapublic class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- public class HelloWorld: Declaration of a class named
HelloWorld
. - public static void main(String[] args): Main method, the entry point of any Java program.
- System.out.println(): Prints the text to the console.
2. Java Data Types and Variables
2.1. Primitive Data Types:
- int: Integer, e.g.,
int age = 25;
- float: Floating-point number, e.g.,
float price = 10.99f;
- double: Double precision floating-point number, e.g.,
double pi = 3.14159;
- char: Character, e.g.,
char grade = 'A';
- boolean: Boolean value,
true
orfalse
, e.g.,boolean isJavaFun = true;
2.2. Non-Primitive Data Types:
- String: Sequence of characters, e.g.,
String name = "John";
- Arrays: Collection of elements of the same type, e.g.,
int[] numbers = {1, 2, 3, 4};
- Classes and Objects: User-defined data types that represent real-world entities.
2.3. Variable Declaration and Initialization:
javaint x = 10;
String message = "Hello, Java!";
3. Control Structures
3.1. Conditional Statements:
- If-Else Statement:java
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
- Switch Statement:java
switch (variable) { case value1: // Code break; case value2: // Code break; default: // Default code }
3.2. Loops:
- For Loop:java
for (int i = 0; i < 5; i++) { // Code to execute repeatedly }
- While Loop:java
while (condition) { // Code to execute as long as condition is true }
- Do-While Loop:java
do { // Code } while (condition);
4. Object-Oriented Programming (OOP) Concepts
4.1. Classes and Objects:
- Class: Blueprint for creating objects. It contains fields (attributes) and methods (functions).java
class Car { String model; int year; void start() { System.out.println("Car started."); } }
- Object: Instance of a class.java
Car myCar = new Car(); myCar.model = "Tesla"; myCar.year = 2020; myCar.start();
4.2. Inheritance:
- Inheritance: Allows a new class to inherit properties and methods from an existing class.java
class Vehicle { void honk() { System.out.println("Vehicle honks!"); } } class Car extends Vehicle { void start() { System.out.println("Car started."); } }
4.3. Polymorphism:
- Polymorphism: The ability of an object to take many forms.java
class Animal { void sound() { System.out.println("Animal makes a sound."); } } class Dog extends Animal { void sound() { System.out.println("Dog barks."); } } class Cat extends Animal { void sound() { System.out.println("Cat meows."); } }
4.4. Encapsulation:
- Encapsulation: Wrapping of data and methods into a single unit, and restricting access to some of the object's components.java
class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.5. Abstraction:
- Abstraction: Hiding the implementation details and exposing only the functionality.java
abstract class Shape { abstract void draw(); } class Circle extends Shape { void draw() { System.out.println("Drawing Circle"); } }
5. Common Java Interview Questions & Answers
5.1. What is Java?
- Java is a high-level, object-oriented programming language that is platform-independent, thanks to the JVM (Java Virtual Machine).
5.2. What are the main features of Java?
- Platform independence, Object-oriented, Simple, Secure, Portable, Robust, Multithreaded, and Dynamic.
5.3. What is the difference between JDK, JRE, and JVM?
- JDK (Java Development Kit): The toolkit that provides tools to develop Java programs, including the compiler.
- JRE (Java Runtime Environment): The environment required to run Java applications, including the JVM and core libraries.
- JVM (Java Virtual Machine): The engine that runs the Java bytecode.
5.4. What are constructors in Java?
- Constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type.java
class Car { String model; Car(String model) { this.model = model; } }
5.5. Explain the concept of Inheritance in Java.
- Inheritance is a mechanism where a new class (child class) inherits properties and behavior (methods) from an existing class (parent class).
5.6. What is method overloading?
- Method overloading allows a class to have more than one method with the same name but different parameters.java
class Math { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }
5.7. What is the difference between ==
and .equals()
in Java?
==
checks for reference equality, i.e., whether two references point to the same object in memory..equals()
is used to check for value equality, i.e., whether two objects are logically "equal."
5.8. What is the purpose of the final
keyword in Java?
final
can be used to mark a variable as constant, a method as unchangeable, or a class as non-inheritable.
5.9. What are exceptions in Java?
- Exceptions are events that disrupt the normal flow of a program. Java uses exception handling to manage errors through
try
,catch
,finally
, andthrow
.
5.10. What is the difference between checked and unchecked exceptions?
- Checked exceptions are checked at compile-time (e.g., IOException).
- Unchecked exceptions are checked at runtime (e.g., ArithmeticException).
5.11. What is the difference between ArrayList and LinkedList in Java?
- ArrayList: Provides fast random access and is better for storing and accessing data.
- LinkedList: Provides better performance for insertion and deletion operations.
5.12. What is a null
pointer exception?
- A
null
pointer exception occurs when you try to use a reference that points tonull
.
5.13. What are access modifiers in Java?
- Public: Accessible from anywhere.
- Private: Accessible only within the declared class.
- Protected: Accessible within the same package and subclasses.
- Default: Accessible only within the same package.
5.14. What is multithreading in Java?
- Multithreading is a Java feature that allows concurrent execution of two or more threads for maximum CPU utilization.