1
JDK, JRE and JVM
★★★★★
↑ Index
Java
Java source code is compiled by javac into bytecode (.class). The JVM runs that bytecode on any operating system, which makes Java platform independent.

JDK contains the JRE, the JRE contains the JVM
Structure of a Java program · Java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
// javac Hello.java → Hello.class
// java Hello → runs on the JVM
Q & A
- Why is main public static void?
public: the JVM must call it from outside. static: it runs without creating an object. void: it returns nothing to the JVM.
- Is Java fully object oriented?
Not 100%, because it has primitive types (int, char, ...) and static members.
2
String, StringBuilder and StringBuffer
★★★★★
↑ Index
Java
| String | StringBuilder | StringBuffer |
|---|
| Immutable | Mutable | Mutable |
| Thread-safe (because immutable) | Not thread-safe | Thread-safe (synchronized) |
| Slow for many changes | Fastest | Slower than StringBuilder |
Java
String s1 = "hello"; // string pool
String s2 = "hello"; // same pool object
String s3 = new String("hello"); // new heap object
System.out.println(s1 == s2); // true (same reference)
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s3)); // true (same content)
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) sb.append(i);
System.out.println(sb.reverse()); // 43210
Q & A
- == vs equals()?
== compares references (same object?). equals() compares content. Always use equals() for strings.
- Why is String immutable?
For security (class names, URLs, passwords), string pool sharing, thread safety and caching the hashCode.
3
Exception Handling in Java
★★★★★
↑ Index
Java
| Checked exception | Unchecked exception |
|---|
| Checked at compile time | Happens at run time |
| Must be caught or declared with throws | Not forced |
| IOException, SQLException, FileNotFoundException | NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException |
Hierarchy: Throwable → Error (OutOfMemoryError, StackOverflowError — do not catch) and Exception → RuntimeException (unchecked).
Java
try {
int[] arr = new int[3];
arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Bad index: " + e.getMessage());
} finally {
System.out.println("finally always runs");
}
// try-with-resources closes the reader automatically
try (BufferedReader br = new BufferedReader(new FileReader("a.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
class InsufficientBalanceException extends Exception { // custom checked exception
InsufficientBalanceException(String msg) { super(msg); }
}
Q & A
- throw vs throws?
throw actually throws one exception object inside a method. throws in the method signature declares which checked exceptions the method may throw.
- final vs finally vs finalize?
final: keyword for constants / no override / no inheritance. finally: block that always runs after try-catch. finalize(): old GC hook, deprecated.
4
Java Collections Framework
★★★★★
↑ Index
Java

List, Set, Queue and Map with their common implementations
| Class | Order | Duplicates | Lookup |
|---|
| ArrayList | Insertion | Yes | get(i) O(1) |
| LinkedList | Insertion | Yes | get(i) O(n) |
| HashSet | None | No | O(1) |
| TreeSet | Sorted | No | O(log n) |
| HashMap | None | Keys unique | O(1) |
| TreeMap | Sorted by key | Keys unique | O(log n) |
Java
List<Integer> list = new ArrayList<>(List.of(5, 2, 8));
Collections.sort(list); // [2, 5, 8]
Map<String, Integer> freq = new HashMap<>();
for (String w : "a b a c a".split(" "))
freq.put(w, freq.getOrDefault(w, 0) + 1);
System.out.println(freq); // {a=3, b=1, c=1}
Set<String> set = new TreeSet<>(List.of("b", "a", "b")); // [a, b]
Q & A
- HashMap vs Hashtable?
HashMap is not synchronized, allows one null key, faster. Hashtable is synchronized (legacy) and allows no null. For threads use ConcurrentHashMap.
- How does HashMap work?
hashCode() picks a bucket; entries in the same bucket form a linked list (a red-black tree after 8 entries); equals() finds the exact key.