Object-oriented programming · interview question
When comparing two String objects, what's the difference between == and equals()?
Multiple choiceEasy
Answer and explanation
Correct answer
A. == compares references (same object); equals() compares the characters.
Why
For objects, == checks identity. String.equals checks content. Always use equals (or Objects.equals for null safety) for value comparison.
References
Test yourself on Object-oriented programming
A free test with instant, explained results. No account needed.
More Object-oriented programming questions
- Which statements about the equals/hashCode contract are true? Select all that apply.
- In modern Java (8+), when would you choose an abstract class over an interface? Compare the two.
- What issue might arise if a constructor directly assigns a mutable object without copying it?
- What happens if a subclass does not implement all abstract methods from its abstract superclass?
- What does the 'this' keyword refer to within an instance method of a class?
- Which Java keyword would you use to declare an abstract class in Java?