Equals () Method Behavior in Java

Consider the following Java code:

Object a = new Integer(2); Object b = new Integer(2); System.out.println(a.equals(b)); Object x = new Object(); Object y = new Object(); System.out.println(x.equals(y)); 

The first print statement prints true and the second false .

If this is intentional behavior, how does this help Java programming?

If this is not intentional behavior, is this a bug in Java?

+8
java
source share
5 answers

I'm going to answer your question with reservations, but you should know that you are hurting yourself if the purpose of the question is for you to learn, and your solution was to ask StackOverflow. That aside ...

This behavior is intentional.

The default equals() method java.lang.Object compares memory addresses, which means that all objects are different from each other (only two references to the same object return true ).

java.lang.Integer overrides this to compare the value of Integer s, so two different Integer representing the number two are compared equal. If you used == instead, you will get false for both cases.

A standard practice in Java is to override the equals method to return true for objects with the same boolean value, even if they were created at different times (or even with different parameters). It is not very useful to have objects representing numbers if you have no opportunity to ask: "Do these two things represent the same meaning?"

By the way, and this is tangent here, Java actually stores the cache of Integer objects for small values. Therefore, sometimes you can get two Integer objects, where even the == operator will return true , despite the fact that you get them from two different sources. You can even get code that behaves differently for large integers than for a smaller size, excluding integral values!

+23
source share

This is the intended behavior.

Object.equals() considers the identifier of the object (i.e. the object is only equal to itself), which is the only thing you can do for shared objects.

Integer overrides the method depending on the integer value, since two Integer objects with the same value are logically equal. Many other classes also override equals() , since this is the main mechanism of the standard API and many functional ones, for example. within the collections depends on him.

Why are you still puzzled by behavior? Most people confuse only the == operator, which does not behave like this (it works like Object.equals() ).

+6
source share

The equals method in Java fulfills a specific purpose: it determines whether objects are logically equal, i.e. their contents are the same, whatever that means in the context of each particular class. This contrasts with the fact that the objects are the same: two different objects can be logically equivalent.

Returning to your example, a and b are different objects that represent the same logical object - the integer value 2 . They model the same concept - an integer, and integer numbers with the same value are identical to each other. a and b are therefore equal.

Objects x and y , on the other hand, do not represent the same logical object (in fact, they do not represent anything). That is why they are not the same and not equivalent.

+2
source share

This is intentional, of course.

When comparing objects, Integer equals returns true if their values ​​(in your case 1 ) are equal.

Applied to different objects of type Object , it returns false .

 x.equals(x) 

will return true .

0
source share

See also Object.equals () doc . By the way, consider using Integer.valueOf(2) rather than new Integer(2) , as this will reduce the memory footprint.

The last funny thing is Integer.valueOf(2)==Integer.valueOf(2) will return true, but Integer.valueOf(2000)==Integer.valueOf(2000) will return false, because in the first case you will get twice the same instance of the object Integer (there is a cache), but not in the second case, because the cache is intended only for values ​​from -127 to 128

0
source share

All Articles