What happens when you compare two objects of the same type using ==,>, <, etc. In Java?

Possible duplicate:
The difference between equals and ==

For example, if I have

 MyClass foo = new MyClass(); MyClass bar = new MyClass(); if (foo == bar) { // do something } if (foo < bar) { // do something } if (foo > bar) { // do something } 

how to compare foo and bar ? Does Java represent .compareTo() methods for MyClass ? Is Java mapping an actual binary object bit structure for bits in memory?

+4
source share
5 answers

Very simple arithmetic comparison operators == and != Compare object references or object memory addresses. > and < , and related operators cannot be used with objects.

So, == != only useful if you want to determine if two different variables point to the same object.

As an example, this is useful in an event handler: if you have one event handler attached to, for example, several buttons, you will need to determine in the handler which button was pressed. In this case, you can use == .

Comparing objects of the type you are asking for is captured using methods such as .equals or special methods such as String.compareTo .

It is worth noting that the Object.equals method is equivalent to == by default: it compares object references; this is described in the docs . Most classes built into Java override equals with their own implementation: for example, String overrides equals to compare characters one at a time. To get a more specific / useful .equals implementation for your own objects, you need to override .equals with a more specific implementation.

+8
source

You have not tried it yourself because < , > , <= and >= do not work with objects.

However == compares the left and right operands. When they are binary, this leads to truth. In the case of objects, pointers are compared. Thus, this means that it will only lead to truth if the object remains left and right with the same object in memory.

Other methods, such as compareTo and equals, are created to provide a custom method for comparing with various objects in memory, but which can be equal to each other (i.e. the data is the same).

In the case of strings, for example:

 String str0 = new String("foo"); String str1 = new String("foo"); // A human being would say that the two strings are equal, which is true // But it are TWO different objects in memory. So, using == will result // in false System.out.println(str0 == str1); // false // But if we want to check the content of the string, we can use the equals method, // because that method compares character by character of the two objects String.out.println(str0.equals(str1)); // true String.out.println(str1.equals(str0)); // true 
+4
source

No no. It compares whether two variables are references to the same objects.

If you are not dealing with types that are subject to autoboxing, such as Integer , you cannot use > and < with objects in general.

In the case of using autobox, java does not search for specific methods, but automatically removes the variables, turning them into primitives, but this does not apply to the equals operator. The == operator will always compare objects as links, even if you compare objects with auto objects:

  Integer i1 = new Integer(10); Integer i2 = new Integer(10); if(i1 < i2) { // evaluates to false! System.out.println("i1 is less than i2"); } else if(i1 > i2) { // evaluates to false! System.out.println("i1 is greater than i2"); } else if(i1 == i2) { // evaluates to false! System.out.println("i1 and i2 are equal"); } else { System.out.println("Um... well that just confusingi"); } 
+2
source

It compares the reference value and will only return true if foo and bar point to the same object.

+1
source

In Java, "==" compares the identifier of an object. "new" is guaranteed to return a new object identifier every time.

I would really love it if "==" called compareTo. Alas, this is not so.

+1
source

Source: https://habr.com/ru/post/1438101/


All Articles