Equality in OOP (== vs .equals), Method Hiding & String Immutability
Reference equality vs content equality, static method hiding, and String vs StringBuilder vs StringBuffer.
Mock tests for SSC, Banking, Railways, UPSC & state exams
Real exam pattern, negative marking and a worked solution for every question.
Equality testing and String manipulation are among the most frequently tested interview topics.
1. The Difference Between == and .equals() in Java#
Tricky Question 5: "What is the difference between == and .equals() in Java?"
• == checks for reference equality (whether two object pointers refer to the exact same physical memory address in the heap).
• .equals() checks for content/value equality (whether two distinct objects have the same internal field values).
2. What is Method Hiding?#
Tricky Question 19: "What is method hiding?"
Method hiding occurs when a subclass defines a static method with the exact same signature as a static method in the superclass. Because static methods are resolved at compile-time (early binding), the subclass method simply hides the superclass method, rather than overriding it.
Practise placement aptitude on the real test pattern
TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.
3. String vs StringBuilder vs StringBuffer#
Tricky Question 26: Comparing the 3 text handling classes:
| Class | Mutability | Thread Safety | Performance |
|---|---|---|---|
| String | Immutable (constant once created) | Thread-Safe by default (read-only) | Slow for heavy concatenations (creates new objects) |
| StringBuilder | Mutable (modifies internal buffer) | Not Thread-Safe | Fastest performance for single-threaded string construction |
| StringBuffer | Mutable (modifies internal buffer) | Thread-Safe (synchronized methods) | Slightly slower due to thread locking synchronization |
4. instanceof vs getClass()#
Tricky Question 22: "What is the difference between instanceof and getClass()?"
• `instanceof` checks if an object is an instance of a specified class OR any of its derived subclasses.
• `getClass()` returns the EXACT runtime class of the object, rejecting subclasses.
Test Your Concept Mastery
Verify your retention on: Equality in OOP (== vs .equals), Method Hiding & String Immutability
Which of the following is mutable and thread-safe?
Ready for a full proctored assessment?
Take a free quick test on the real exam pattern, with a worked solution for every question.