JAVA Hand Book
JAVA Hand Book
1. Introduction to Java
Java is a versatile, object-oriented programming language known for its platform
independence, reliability, and extensive libraries. Java programs run on the Java Virtual
Machine (JVM), making them highly portable across different systems.
java
4. Operators in Java
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Switch Statement
java
Copy code
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
Loops
For Loop:
java
Copy code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
While Loop:
java
Copy code
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Do-While Loop:
java
Copy code
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
6. Methods
Methods are reusable blocks of code in Java that perform specific tasks.
java
Copy code
public static int add(int a, int b) {
return a + b;
}
Method Call:
java
Copy code
int sum = add(5, 10);
Method Overloading
Allows multiple methods with the same name but different parameters.
java
Copy code
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
java
Copy code
public class Car {
String model;
int year;
Inheritance
java
Copy code
public class Animal {
public void eat() {
System.out.println("Eating...");
}
}
Polymorphism
Method Overriding: Allows a subclass to provide a specific implementation of a
method in the superclass.
Encapsulation
Encapsulation protects the data within a class by marking fields as private and providing
getters and setters.
java
Copy code
public class Person {
private String name;
Abstraction
java
8. Exception Handling
java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This block always executes");
}
java
Copy code
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
java
Copy code
Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
uniqueNames.add("Bob");
java
Copy code
Map<Integer, String> idToName = new HashMap<>();
idToName.put(1, "Alice");
idToName.put(2, "Bob");
java
Copy code
try {
File file = new File("example.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
}
11. Multithreading
Java allows concurrent execution through threads.
java
Copy code
public class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
Runnable Interface
java
Copy code
public class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running");
}
}
This handbook provides a summary of Java’s foundational elements, from syntax and OOP
principles to advanced concepts like collections, file handling, and multithreading.
Experimenting with these concepts will reinforce your understanding and proficiency in Java
programming.