∑GateAndNet
PlannerSign Up
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 = 9 inside a method persists, whereas reassigning obj = 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 Integer Cache (-128 to 127): Integer a = 127, b = 127; (a == b) is true (cached by Integer.valueOf), whereas Integer c = 128, d = 128; (c == d) is false (distinct Heap objects)!

2. Abstract Class vs Interface (Java 8+ / 17+) & Access Modifiers

ModifierSame ClassSame PackageSubclass (Other Pkg)World (Everywhere)
private✅❌❌❌
default (package-private)✅✅❌❌
protected✅✅✅❌
public✅✅✅✅
  • In Java, all non-static, non-private, non-final instance methods are polymorphic (virtual) by default!
  • Checked vs Unchecked Exceptions: Subclasses of RuntimeException (NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException) and Error are Unchecked; other Exception subclasses (IOException, SQLException) are Checked (must declare throws or catch).
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
a.equals(b)=true  ⟹  a.hashCode()==b.hashCode()(Converse NOT required!)a.\texttt{equals}(b) = \texttt{true} \implies a.\texttt{hashCode}() == b.\texttt{hashCode}() \quad (\text{Converse NOT required!})
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
Previous TopicC++ & OOP Internals: Virtual Tables (vptr/vtable), Constructors, Diamond Inheritance & STLNext Topic JavaScript (ES6+) Runtime: Event Loop (Microtasks vs Macrotasks), Closures, Hoisting & Coercion