INTERMEDIATEFrequently Tested 4 min read 3 views
Java & JVM Architecture: String Pool, Integer Cache, Interfaces, Exceptions & Concurrency
Pass-by-value object references, String Constant Pool vs new String(), Integer缓存 [-128, 127], equals/hashCode contract, Java 8+ default interface methods, and try-catch-finally return overrides.
1. Pass-by-Value References, String Pool & Integer Cache Traps
- Java is 100% Pass-by-Value: For objects, the reference value (address) is passed by value — mutating
obj.field = 9inside a method persists, whereas reassigningobj = new Foo()inside the method does NOT change the caller’s reference! - String Constant Pool vs Heap:
java
String s1 = "GATE"; String s2 = "GATE"; String s3 = new String("GATE"); System.out.println((s1 == s2) + " " + (s1 == s3) + " " + s1.equals(s3)); // Prints: true false true (s3.intern() == s1 is true!) - Autoboxed
IntegerCache (-128to127):Integer a = 127, b = 127; (a == b)istrue(cached byInteger.valueOf), whereasInteger c = 128, d = 128; (c == d)isfalse(distinct Heap objects)!
2. Abstract Class vs Interface (Java 8+ / 17+) & Access Modifiers
| Modifier | Same Class | Same Package | Subclass (Other Pkg) | World (Everywhere) |
|---|---|---|---|---|
private | ✅ | ❌ | ❌ | ❌ |
| default (package-private) | ✅ | ✅ | ❌ | ❌ |
protected | ✅ | ✅ | ✅ | ❌ |
public | ✅ | ✅ | ✅ | ✅ |
- In Java, all non-
static, non-private, non-finalinstance methods are polymorphic (virtual) by default! - Checked vs Unchecked Exceptions: Subclasses of
RuntimeException(NullPointerException,ArithmeticException,ArrayIndexOutOfBoundsException) andErrorare Unchecked; otherExceptionsubclasses (IOException,SQLException) are Checked (must declarethrowsorcatch).
Common Exam Mistake
try-finally Return Trap: finally ALWAYS executes before the method returns (unless System.exit(0) or JVM crash occurs). If both try and finally contain a return statement, the finally return silently overrides the try return (and even swallows uncaught exceptions)!
Key Formulas Summary
Java Object Identity vs Value Equality & Hash Contract
Note: Overriding equals() without overriding hashCode() breaks HashMap / HashSet lookups!
#Java#JVM#String Pool#OOP#Exceptions#Multithreading#UGC NET CS
Practice Questions on This Chapter (2)
Q1UGC NET CS June 2024MCQMEDIUM
+2M • 0 Neg
What does the Java expression String a = "NET"; String b = new String("NET"); System.out.print((a == b) + ":" + (a == b.intern())); print?
⚡ Click an option above to verify instantly
Q2UGC NET CS Dec 2022MCQMEDIUM
+2M • 0 Neg
In Java, what integer value is returned by static int test() { try { return 10; } finally { return 20; } }?
⚡ Click an option above to verify instantly