Sobes.tech
Junior — Middle

Kuidas Java-s teostatakse objektide võrdlus võrdusmärgi == abil?

sobes.tech AI

Vastus AI-lt

In Java, when comparing objects, the == operator checks whether both variables refer to the same object in memory, i.e., it compares references and not the content of the objects.

If you want to compare the content of objects, use the equals() method, which can be overridden in the class to define the comparison logic.

Example:

String a = new String("test");
String b = new String("test");

System.out.println(a == b);       // false, different objects
System.out.println(a.equals(b));  // true, same content

Therefore, the == operator for objects checks reference equality, not values.