Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using == operator in Java to compare wrapper objects

I'm reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book is confusing me so much. On page 245 they state that the following code below.

Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects");  //Prints output different objects 

Then on the very next page they have the following code

Integer i3 = 10; Integer i4 = 10; if(i3 == i4) System.out.println("same objects");  //Prints output same objects 

I'm so confused! When I try this out on my own it seems that you cannot use the == to compare the same way you would use equals() method. Using the == always gives me 'false' even if the Integer variables are set to the same value (i.e. 10). Am I correct? Using the == to compare the same Integer object (with same values) will always result in 'false'

like image 855
dido Avatar asked Apr 14 '12 00:04

dido


People also ask

Can we compare wrapper classes using == in Java?

When a wrapper is compared with a primitive using ==, the wrapper is unwrapped. That's true! But which method is invoked/called depends on the type of the wrapper. If your wrapper is of type Integer, then indeed the intValue method will be called.

Can you use == for objects?

In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.

How can the values of wrapper class objects be compared?

Comparing Integers Integer is a wrapper class of int, and it provides several methods and variables you can use in your code to work with integer variables. One of the methods is the compareTo() method. It is used to compare two integer values. It will return a -1, 0, or 1, depending on the result of the comparison.

What is the difference between equals () and == when comparing objects in Java?

In java both == and equals() method is used to check the equality of two variables or objects. == is a relational operator which checks if the values of two operands are equal or not, if yes then condition becomes true. equals() is a method available in Object class and is used to compare objects for equality.


2 Answers

The key to the answer is called object interning. Java interns small numbers (less than 128), so all instances of Integer(n) with n in the interned range are the same. Numbers greater than or equal to 128 are not interned, hence Integer(1000) objects are not equal to each other.

like image 179
Sergey Kalinichenko Avatar answered Sep 23 '22 08:09

Sergey Kalinichenko


If you look at the source code for Integer you'll see that Integer.valueOf(int) pools all values -128 to 127. The reason is that small Integer values are used frequently and are thus worthy of being pooled/cached.

Taken straight from Integer.java:

public static Integer valueOf(int i) {     if(i >= -128 && i <= IntegerCache.high)         return IntegerCache.cache[i + 128];     else         return new Integer(i); } 

Note that this pooling is implementation specific and there's no guarantee of the pooled range.

The answers about interning are correct in concept, but incorrect with terminology. Interning in Java normally implies that the Java runtime is performing the pooling (such as String's intern). In Integer's case it's the class itself that is doing the pooling. There's no JVM magic involved.

like image 41
Steve Kuo Avatar answered Sep 20 '22 08:09

Steve Kuo