4. Java Strings, Exception Handling & Collections
Chapters 14-16: String Pool, StringBuilder, try-catch-finally, Checked vs Unchecked exceptions, ArrayList, HashSet & HashMap
Module 1: Strings, Immutability & StringBuilder
Understanding String memory pooling, immutability, and mutable StringBuilder operations.
🇬🇧 English Definition
String objects in Java are immutable sequence of characters managed inside String Constant Pool. StringBuilder provides mutable character sequences for efficient string concatenations.
🇮🇳 Hinglish Definition
String immutable (unchangeable) hoti hai aur String Pool me store hoti hai. Repeated modifications ke liye StringBuilder efficient hai.
Core Concepts & Rules
- equals() vs ==: equals() compares string text content; == compares object memory references.
- String Immutability: Modifying a string creates a new object instance leaving original untouched.
🔍 Line-by-Line Code Breakdown
- Line 2-3: String a = new String('Java') and String b = new String('Java') create separate objects.
- Line 4: a.equals(b) returns true (content match); a == b returns false (different references).
🎯 Interview Focus & Questions
- Q: Why is String immutable in Java?
A: Security, thread-safety, caching hashcodes, and String Pool memory optimization. - Q: equals() vs ==?
A: equals() compares logical content; == compares memory reference addresses.
public class StringDemo {
public static void main(String[] args) {
String a = new String("Java");
String b = new String("Java");
System.out.println(a.equals(b)); // true
System.out.println(a == b); // false
StringBuilder sb = new StringBuilder();
sb.append("Java").append(" Notes");
System.out.println(sb.toString());
}
}
Module 2: Exception Handling & Collections Framework
Managing runtime errors with try-catch-finally and utilizing Collections (ArrayList, HashSet, HashMap).
🇬🇧 English Definition
Exceptions represent abnormal conditions. Checked exceptions require compiler handling; Unchecked extend RuntimeException. Collections Framework provides ArrayList, HashSet, and HashMap data structures.
🇮🇳 Hinglish Definition
Error ko controlled way me handle karne ke liye try-catch-finally use karte hain. Collections Framework ArrayList, HashSet aur HashMap jaise data structures deta hai.
Core Concepts & Rules
- try-catch-finally: try encloses risky code, catch handles matching exception, finally executes cleanup code.
- List vs Set vs Map: List keeps ordered duplicates; Set stores unique elements; Map holds key-value pairs.
🔍 Line-by-Line Code Breakdown
- Line 2-4: try-catch block handling ArithmeticException divide by zero error.
- Line 7-10: HashMap storing ID to Name key-value mappings.
🎯 Interview Focus & Questions
- Q: Checked vs Unchecked exception?
A: Checked exceptions are verified at compile-time; Unchecked extend RuntimeException and occur at runtime. - Q: ArrayList vs LinkedList?
A: ArrayList uses dynamic array with fast indexed access O(1); LinkedList uses doubly-linked nodes.
import java.util.*;
public class ExceptionCollectionDemo {
public static void main(String[] args) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
Map<Integer, String> map = new HashMap<>();
map.put(101, "Aman");
System.out.println("Student: " + map.get(101));
}
}