📘 Java Complete Notes
📌 1. Introduction to Java
Developed by James Gosling at Sun Microsystems (1995).
Platform-independent, Object-Oriented, Secure, and Multithreaded.
Runs on the Java Virtual Machine (JVM).
🔤 2. Basic Syntax
java
CopyEdit
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class: Class definition.
main() method: Entry point.
System.out.println: Output statement.
🔠 3. Data Types
Primitive: int, byte, short, long, float, double, char, boolean
Non-Primitive: String, Arrays, Objects
➕ 4. Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, <, >, <=, >=
Logical: &&, ||, !
Assignment: =, +=, -=, etc.
Bitwise: &, |, ^, ~, <<, >>
Ternary: condition ? expr1 : expr2
🔁 5. Control Flow Statements
Conditional: if, else, switch
Loops: for, while, do-while
Jump: break, continue, return
🧠 6. Methods (Functions)
java
CopyEdit
int add(int a, int b) {
return a + b;
}
Parameters, return types, method overloading.
Static vs non-static methods.
📦 7. Arrays & Strings
Arrays:
java
CopyEdit
int[] arr = new int[5];
int[] arr = {1, 2, 3};
Strings:
java
CopyEdit
String s = "Hello";
s.length(); s.charAt(0); s.substring(1);
📌 8. Object-Oriented Programming (OOP)
Class and Object:
java
CopyEdit
class Car {
String color;
void drive() { System.out.println("Driving"); }
}
Encapsulation: private fields + public getters/setters.
Inheritance: class Dog extends Animal
Polymorphism: Method overloading and overriding.
Abstraction:
o Abstract class: abstract class Shape { abstract void draw(); }
o Interface: interface Drawable { void draw(); }
Constructor and Destructor:
java
CopyEdit
Car() { }
🔄 9. Inheritance Types
Single
Multilevel
Hierarchical
Multiple (via Interfaces)
⚙️ 10. Exception Handling
java
CopyEdit
try {
int x = 1 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e);
} finally {
System.out.println("Always executes");
}
Checked vs Unchecked exceptions.
Custom exceptions via subclassing Exception.
🧠 11. File Handling
java
CopyEdit
import java.io.*;
FileWriter fw = new FileWriter("file.txt");
fw.write("Hello");
fw.close();
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
br.close();
📚 12. Collections Framework
List (ArrayList, LinkedList)
Set (HashSet, TreeSet)
Map (HashMap, TreeMap)
Queue, Deque
Common operations:
java
CopyEdit
list.add(1); set.contains(2); map.get(key);
🧠 13. Generics
java
CopyEdit
class Box<T> {
T value;
void set(T v) { value = v; }
T get() { return value; }
}
Ensures type safety and code reusability.
🧠 14. Multithreading
java
CopyEdit
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
Thread, Runnable, synchronized, wait(), notify()
🛠 15. Interfaces and Abstract Classes
java
CopyEdit
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}
Abstract class can have implementation; interfaces cannot (pre-Java 8).
🗃 16. Packages
Group related classes:
java
CopyEdit
package mypackage;
import mypackage.MyClass;
🌐 17. Java API & Libraries
java.lang: Core classes
java.util: Collections, Date, Scanner
java.io: File handling
java.net: Networking
java.sql: JDBC
javax.swing: GUI programming
🌟 18. Java 8+ Features
Lambda Expressions:
java
CopyEdit
(a, b) -> a + b
Streams API
Functional Interfaces: @FunctionalInterface
Default & Static methods in interfaces
Optional, Date and Time API (java.time)
💡 19. Java Memory Management
Heap and Stack
Garbage Collection
finalize() method
System.gc()
🧠 20. Design Patterns (Intro)
Singleton
Factory
Observer
MVC
Strategy
🧠 21. Unit Testing with JUnit
java
CopyEdit
@Test
public void testAdd() {
assertEquals(5, calc.add(2, 3));
}