# **Detailed Notes on Java Programming**
## **Table of Contents**
1. [Introduction to Java](#1-introduction-to-java)
2. [Java Syntax & Structure](#2-java-syntax--structure)
3. [Data Types & Variables](#3-data-types--variables)
4. [Operators](#4-operators)
5. [Control Flow Statements](#5-control-flow-statements)
6. [Methods (Functions)](#6-methods-functions)
7. [Arrays](#7-arrays)
8. [Object-Oriented Programming (OOP)](#8-object-oriented-programming-oop)
9. [Exception Handling](#9-exception-handling)
10. [File Handling](#10-file-handling)
11. [Collections Framework](#11-collections-framework)
12. [Multithreading](#12-multithreading)
---
## **1. Introduction to Java**
- **Developed by**: James Gosling (Sun Microsystems, 1995)
- **Key Features**:
- **Platform-independent** (Write Once, Run Anywhere - WORA)
- **Object-Oriented** (Encapsulation, Inheritance, Polymorphism, Abstraction)
- **Robust & Secure** (Automatic garbage collection, exception handling)
- **Multithreading support**
- **Java Editions**:
- **Java SE (Standard Edition)** – Core Java
- **Java EE (Enterprise Edition)** – Web & Enterprise apps
- **Java ME (Micro Edition)** – Mobile & embedded systems
---
## **2. Java Syntax & Structure**
### **Basic Java Program Structure**
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
- **`public class HelloWorld`**: Class name must match the filename (`HelloWorld.java`).
- **`public static void main(String[] args)`**: Entry point of the program.
- **`System.out.println()`**: Prints output to the console.
---
## **3. Data Types & Variables**
### **Primitive Data Types**
| Data Type | Size | Example |
|-----------|------|---------|
| `byte` | 1 byte | `byte b = 100;` |
| `short` | 2 bytes | `short s = 1000;` |
| `int` | 4 bytes | `int num = 100000;` |
| `long` | 8 bytes | `long l = 100000L;` |
| `float` | 4 bytes | `float f = 3.14f;` |
| `double` | 8 bytes | `double d = 3.14159;` |
| `char` | 2 bytes | `char c = 'A';` |
| `boolean` | 1 bit | `boolean flag = true;` |
### **Non-Primitive (Reference) Data Types**
- `String`, `Arrays`, `Classes`, `Interfaces`
### **Variable Declaration**
```java
int age = 25;
String name = "Alice";
final double PI = 3.14159; // Constant (final keyword)
```
---
## **4. Operators**
| Category | Operators |
|----------|-----------|
| Arithmetic | `+`, `-`, `*`, `/`, `%`, `++`, `--` |
| Relational | `==`, `!=`, `>`, `<`, `>=`, `<=` |
| Logical | `&&`, `||`, `!` |
| Assignment | `=`, `+=`, `-=`, `*=`, `/=`, `%=` |
| Bitwise | `&`, `|`, `^`, `~`, `<<`, `>>`, `>>>` |
---
## **5. Control Flow Statements**
### **If-Else**
```java
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
```
### **Switch-Case**
```java
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid day");
}
```
### **Loops**
```java
// For Loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// While Loop
while (i < 5) {
System.out.println(i);
i++;
}
// Do-While Loop
do {
System.out.println(i);
i++;
} while (i < 5);
```
---
## **6. Methods (Functions)**
### **Method Definition**
```java
public static int add(int a, int b) {
return a + b;
}
```
### **Method Overloading**
```java
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
```
---
## **7. Arrays**
### **1D Array**
```java
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
```
### **2D Array**
```java
int[][] matrix = {{1, 2}, {3, 4}};
System.out.println(matrix[0][1]); // Output: 2
```
---
## **8. Object-Oriented Programming (OOP)**
### **Class & Object**
```java
class Car {
String model;
void start() {
System.out.println("Car started!");
}
}
Car myCar = new Car();
myCar.model = "Tesla";
myCar.start();
```
### **Inheritance**
```java
class Vehicle { }
class Car extends Vehicle { }
```
### **Polymorphism**
```java
// Method Overriding
class Animal { void sound() { } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
```
### **Encapsulation (Getters & Setters)**
```java
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
```
### **Abstraction (Abstract Class & Interface)**
```java
abstract class Animal { abstract void sound(); }
interface Drivable { void drive(); }
```
---
## **9. Exception Handling**
```java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("Cleanup code");
}
```
---
## **10. File Handling**
```java
import java.io.*;
// Writing to a file
FileWriter writer = new FileWriter("file.txt");
writer.write("Hello Java!");
writer.close();
// Reading from a file
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String line = reader.readLine();
reader.close();
```
---
## **11. Collections Framework**
| Interface | Implementation |
|-----------|----------------|
| `List` | `ArrayList`, `LinkedList` |
| `Set` | `HashSet`, `TreeSet` |
| `Map` | `HashMap`, `TreeMap` |
### **Example: ArrayList**
```java
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println(list.get(0)); // Output: Java
```
---
## **12. Multithreading**
### **Thread Creation (Extending Thread Class)**
```java
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
MyThread t1 = new MyThread();
t1.start();
```
### **Thread Creation (Implementing Runnable)**
```java
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running");
}
}
Thread t2 = new Thread(new MyRunnable());
t2.start();
```