Junior — Middle
How can it be determined that two objects refer to the same instance in memory?
sobes.tech AI
Answer from AI
In Android (Java/Kotlin), to check whether two objects refer to the same instance in memory, the == operator (in Kotlin) or the == operator in Java for reference comparison is used (in Java, == is used for reference comparison, and the equals() method for content comparison).
Example in Kotlin:
val obj1 = SomeClass()
val obj2 = obj1
val obj3 = SomeClass()
println(obj1 == obj2) // true, because references are the same
println(obj1 == obj3) // false, different instances
In Java:
SomeClass obj1 = new SomeClass();
SomeClass obj2 = obj1;
SomeClass obj3 = new SomeClass();
System.out.println(obj1 == obj2); // true
System.out.println(obj1 == obj3); // false
Thus, the == operator compares references, while the equals() method compares object contents.