Java Handwritten Short Notes
1. Java Basics
Java: High-level, object-oriented, platform-independent language
WORA: Write Once, Run Anywhere (via JVM)
Developed by Sun Microsystems (1995), now Oracle
2. JVM, JRE, JDK
JDK (Development Kit)
└── JRE (Runtime Environment)
└── JVM (Virtual Machine) + Libraries
Component Purpose
JVM Executes bytecode
JRE JVM + Libraries
JDK JRE + Compiler + Tools
3. Data Types
Primitive (8 types)
Type Size Range
byte 1 byte -128 to 127
short 2 bytes -32K to 32K
int 4 bytes -2B to 2B
long 8 bytes -9Q to 9Q
float 4 bytes 6-7 decimal digits
double 8 bytes 15 decimal digits
char 2 bytes single character
boolean 1 bit true/false
4. Variables & Operators
int age = 25;
double salary = 50000.50;
char grade = 'A';
String name = "John";
boolean active = true;
Operators: + - * / % == != > < >= <= && || !
5. Control Statements
If-Else
if (condition) {
// code
} else if (condition2) {
// code
} else {
// code
}
Switch
switch (expr) {
case value1: code; break;
case value2: code; break;
default: code;
}
Loops
for (int i = 0; i < 5; i++) { }
while (condition) { }
do { } while (condition);
for (Type var : array) { } // enhanced for
6. Arrays
int[] arr = new int[5];
int[] arr = {1, 2, 3, 4, 5};
int[][] matrix = new int[3][3];
7. OOPs Concepts
Four Pillars
1. Encapsulation: Data hiding + methods
2. Inheritance: Parent-Child relationship
3. Polymorphism: Many forms (overloading, overriding)
4. Abstraction: Hide implementation
Class & Object
class Student {
String name; // field
Student(String n) { // constructor
name = n;
}
void display() { } // method
}
Student s = new Student("John");
Inheritance
class Parent { }
class Child extends Parent { }
Polymorphism
Overloading (Compile-time)
int add(int a, int b) { }
int add(int a, int b, int c) { }
Overriding (Runtime)
class Child extends Parent {
@Override
void show() { }
}
Encapsulation
class Employee {
private int salary;
public int getSalary() { return salary; }
public void setSalary(int s) { salary = s; }
}
Abstraction
Abstract Class
abstract class Shape {
abstract void draw(); // abstract
void display() { } // concrete
}
Interface
interface Drawable {
void draw();
default void print() { } // Java 8+
}
8. Keywords
Keyword Use
static Belongs to class
final Constant/cannot change
this Current object
super Parent class
new Create object
instanceof Check type
9. Exception Handling
try {
// risky code
} catch (SpecificException e) {
// handle
} catch (Exception e) {
// generic
} finally {
// always executes
}
Throw: throw new Exception("msg");
Throws: void method() throws Exception { }
10. String Handling
String (Immutable)
String s = "Hello";
[Link](); // 5
[Link](0); // 'H'
[Link](0,3); // "Hel"
[Link](s2); // compare
[Link](); // "HELLO"
[Link](); // remove spaces
[Link](" "); // array
StringBuilder (Mutable, faster)
StringBuilder sb = new StringBuilder("Hello");
[Link](" World");
[Link]();
11. Collections
List
ArrayList<String> list = new ArrayList<>();
[Link]("A");
[Link](0);
[Link](0);
[Link]();
Set
HashSet<Integer> set = new HashSet<>(); // unordered
LinkedHashSet<Integer> lhs = new LinkedHashSet<>(); // insertion order
TreeSet<Integer> ts = new TreeSet<>(); // sorted
Map
HashMap<String, Integer> map = new HashMap<>();
[Link]("key", 100);
[Link]("key");
[Link]("key");
12. Generics
class Box<T> {
T value;
void set(T v) { value = v; }
T get() { return value; }
}
Box<String> b = new Box<>();
13. Java 8 Features
Lambda
// (params) -> body
Runnable r = () -> [Link]("Hello");
Stream API
[Link]()
.filter(n -> n > 5)
.map(n -> n * 2)
.forEach([Link]::println);
14. Multithreading
// Extend Thread
class MyThread extends Thread {
public void run() { }
}
new MyThread().start();
// Implement Runnable
new Thread(() -> { }).start();
Methods: start() run() sleep() join() yield()
15. File I/O
// Read
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while ((line = [Link]()) != null) { }
// Write
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]"));
[Link]("text");
// Try-with-resources
try (BufferedReader br = new BufferedReader(...)) { }
16. JDBC
[Link]("[Link]");
Connection con = [Link](url, user, pass);
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM table");
while ([Link]()) { [Link]("id"); }
Quick Reference
Task Code
Main method public static void main(String[] args)
Print [Link]()
Input Scanner sc = new Scanner([Link])
Array length [Link]
String length [Link]()
Compare strings [Link](str2)
To string [Link](num)
To int [Link](str)
Random [Link]()
Interview Q&A
Q: == vs equals()?
A: == compares references, equals() compares content
Q: ArrayList vs LinkedList?
A: ArrayList = fast access, slow insert; LinkedList = slow access, fast insert
Q: final vs finally vs finalize?
A: final=constant, finally=exception block, finalize=GC method
Q: HashMap vs Hashtable?
A: HashMap=not synchronized, Hashtable=synchronized
Q: What is garbage collection?
A: Automatic memory cleanup of unreferenced objects