Java Core Fundamentals Interview Q&A
== vs equals()
Section titled “== vs equals()”== behaves differently depending on the type:
- For primitive types, it compares values.
- For objects, it compares references (memory addresses).
equals() compares logical equality (content), provided the class overrides it.
String s1 = new String("hello");String s2 = new String("hello");
System.out.println(s1 == s2); // falseSystem.out.println(s1.equals(s2)); // trueSenior-Level Notes
Section titled “Senior-Level Notes”Object.equals()behaves like==unless overridden.Stringoverridesequals()to compare characters.- Mention String Pool behavior:
"abc" == "abc"istruebecause of interning.new String("abc") == new String("abc")isfalse.
JVM Memory Areas
Section titled “JVM Memory Areas”The JVM runtime memory consists of:
- Heap
- Stack
- Metaspace
- Program Counter Register
- Native Method Stack
flowchart TD
JVM --> Heap
JVM --> Stack
JVM --> Metaspace
JVM --> PC["Program Counter"]
JVM --> Native["Native Method Stack"]
- Objects
- Arrays
- String Constant Pool
- Young Generation
- Old Generation
- Method frames
- Local variables
- Object references
Senior-Level Notes
Section titled “Senior-Level Notes”Mention Young Generation, Old Generation, and Metaspace replacing PermGen (Java 8).
Why String Is Immutable
Section titled “Why String Is Immutable”Reasons:
- String Pool optimization
- Thread safety
- Security
- HashCode caching
Methods like concat() create new objects rather than mutating the original.
ArrayList vs LinkedList
Section titled “ArrayList vs LinkedList”| Feature | ArrayList | LinkedList |
|---|---|---|
| Access | O(1) | O(n) |
| Insert/Delete | O(n) | O(1) (known node) |
| Memory | Less | More |
| Random Access | Yes | No |
Use ArrayList for read-heavy workloads.
HashMap vs Hashtable
Section titled “HashMap vs Hashtable”| Feature | HashMap | Hashtable |
|---|---|---|
| Thread Safe | No | Yes |
| Null Keys | One | None |
| Null Values | Many | None |
Prefer ConcurrentHashMap over Hashtable.
final vs finally vs finalize()
Section titled “final vs finally vs finalize()”- final
- variable cannot be reassigned
- method cannot be overridden
- class cannot be extended
- finally
- always executes after try/catch
- used for resource cleanup
- finalize()
- deprecated
- avoid using
Exception Handling
Section titled “Exception Handling”Checked Exceptions:
IOExceptionSQLException
Unchecked Exceptions:
NullPointerExceptionArrayIndexOutOfBoundsException
Use try-with-resources whenever possible.
Abstract Class vs Interface
Section titled “Abstract Class vs Interface”| Abstract Class | Interface |
|---|---|
| Constructors | No constructors |
| State allowed | Constants only |
| Single inheritance | Multiple inheritance |
Java 8+ interfaces support default and static methods.
Functional Interfaces
Section titled “Functional Interfaces”Must contain exactly one abstract method.
Examples: Predicate, Consumer, Supplier, Function, BiFunction.
@FunctionalInterfaceinterface Calculator { int add(int a, int b);}volatile
Section titled “volatile”Guarantees:
- Visibility
- Prevents instruction reordering
Does NOT guarantee:
- Atomicity
Key Takeaways
Section titled “Key Takeaways”==compares references for objects;equals()compares content — but only if the class overrides it. String literals share a pool, so==can be misleadinglytruefor literals butfalsefornew String(...).- The heap holds objects/arrays (further split into Young/Old generation); the stack holds method frames and local variables; Metaspace replaced PermGen in Java 8.
ArrayListwins on random access (O(1));LinkedListwins on insertion/deletion at a known node (O(1)) — choose based on the access pattern, not habit.volatileguarantees visibility and ordering across threads but never atomicity —count++on avolatilefield is still a race condition.