Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these == but not `equals()`?

Tags:

I'm a bit confused about the way Java treats == and equals() when it comes to int, Integer and other types of numbers. For example:

Integer X = 9000; int x = 9000; Short Y = 9000; short y = 9000; List<Boolean> results = new ArrayList<Boolean>(); // results.add(X == Y); DOES NOT COMPILE        1) results.add(Y == 9000);                      // 2) results.add(X == y);                         // 3) results.add(X.equals(x));                    // 4) results.add(X.equals(Y));                    // 5) results.add(X.equals(y));                    // 6) System.out.println(results); 

outputs (maybe you should make your guess first):

[true, true, true, false, false] 
  1. That X == Y does not compile is to be expected, being different objects.
  2. I'm a little surprised that Y == 9 is true, given that 9 is by default an int, and given that 1) didn't even compile. Note that you can't put an int into a method expecting a Short, yet here they are equal.
  3. This is surprising for the same reason as two, but it seems worse.
  4. Not surprising, as x is autoboxed to and Integer.
  5. Not surprising, as objects in different classes should not be equal().
  6. What?? X == y is true but X.equals(y) is false? Shouldn't == always be stricter than equals()?

I'd appreciate it if anyone can help me make sense of this. For what reason do == and equals() behave this way?

Edit: I have changed 9 to 9000 to show that this behavior is not related to the any unusual ways that the integers from -128 to 127 behave.

2nd Edit: OK, if you think you understand this stuff, you should consider the following, just to make sure:

Integer X = 9000; Integer Z = 9000; short y = 9000; List<Boolean> results = new ArrayList<Boolean>(); results.add(X == Z);                      // 1) results.add(X == y);                      // 2) results.add(X.equals(Z));                 // 3) results.add(X.equals(y));                 // 4) System.out.println(results); 

outputs:

[false, true, true, false] 

The reason, as best as I understand it:

  1. Different instance, so different.
  2. X unboxed, then same value, so equal.
  3. Same value, so equal.
  4. y cannot be boxed to an Integer so cannot be equal.
like image 846
Eric Wilson Avatar asked Aug 11 '09 11:08

Eric Wilson


People also ask

Is == the same as equals?

In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects. If a class does not override the equals method, then by default, it uses the equals(Object o) method of the closest parent class that has overridden this method.

What is the difference between equals () and ==?

equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

What does === mean?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

What is the opposite of == in Java?

!=( pronounced not equal to) is the opposite of the equality(==) operator. It will evaluate to true if the values of the two operands are different. It is a relational operator. !=


1 Answers

(small) Integer instances are cached, so the invariant x == y is holded for small instances (actually -127 +128, depends on JVM):

Integer a = 10; Integer b = 10;  assert(a == b); // ok, same instance reused  a = 1024; b = 1024;  assert(a == b); // fail, not the same instance.... assert(a.equals(b)); // but same _value_ 

EDIT

4) and 5) yield false because equals check types: X is an Integer whereas Y is a Short. This is the java.lang.Integer#equals method:

public boolean equals(Object obj) {     if (obj instanceof Integer) {         return value == ((Integer)obj).intValue();     }      return false; } 
like image 170
dfa Avatar answered Oct 06 '22 02:10

dfa