# Java & JVM Architecture: String Pool, Integer Cache, Interfaces, Exceptions & Concurrency
Exam: UGC NET Computer Science & Applications (Code 87) (UGC NET CS)
Subject: Unit 3: Programming in C, C++, Java, JavaScript & Assembly
Canonical URL: https://gateandnet.com/exams/ugc-net-cs/programming-languages/java-jvm-memory-string-pool-interfaces-and-exceptions

> 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

| Modifier | Same Class | Same Package | Subclass (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`).

> [!TRAP]
> **`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)!

## Solved Practice Questions & PYQs

### [UGC NET CS June 2024 | MCQ]
What does the Java expression `String a = "NET"; String b = new String("NET"); System.out.print((a == b) + ":" + (a == b.intern()));` print?
**Correct Answer:** B
**Solution:** `a` points to the String Constant Pool literal `"NET"`, while `new String("NET")` allocates a distinct Heap object (`a == b` is `false`). Calling `b.intern()` returns the canonical pooled reference (`a == b.intern()` is `true`)!

### [UGC NET CS Dec 2022 | MCQ]
In Java, what integer value is returned by `static int test() { try { return 10; } finally { return 20; } }`?
**Correct Answer:** B
**Solution:** In Java, `finally` executes before the `try` block’s `return 10` completes; an explicit `return 20` inside `finally` replaces the pending return value with **`20`**!
