Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer i=3 vs Integer i= new Integer (3) [duplicate]

Tags:

java

I am comparing 2 pieces of code. First

Integer i=3;
Integer j=3;
if(i==j)
   System.out.println("i==j");  //prints i==j              

Second,

Integer i=3;
Integer j=new Integer(3);
if(i==j)
   System.out.println("i==j"); // does not print

I have doubt that in the first snippet why i==j is being printed? Shouldn't the references be different?

like image 664
Shruti Rawat Avatar asked Jul 05 '13 17:07

Shruti Rawat


People also ask

What does New Integer mean in Java?

new Integer(123) will create a new Object instance for each call. According to the javadoc, Integer. valueOf(123) has the difference it caches Objects so you may (or may not) end up with the same Object if you call it more than once.

Can we use .equals for Integer?

Integer Equals() method in JavaThe Equals() method compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

How do you create a new Integer in Java?

To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).

What is the difference between int and Integer?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.


3 Answers

I have doubt that in the first snippet why i==j is being printed? Shouldn't the references be different?

Because,

    Integer i=3;
    Integer j=3;

are internally using Integer#valueOf() to perform autoBoxing . And oracle doc says about valueOf() method that:

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Since the value 3 is cached therefore, both variables i and j are referencing the same object. So, i==j is returning true. Integer#valueOf() uses flyweight pattern.

like image 177
Vishal K Avatar answered Nov 15 '22 17:11

Vishal K


Integer i=3;
Integer j=3;
if(i==j)System.out.println("i==j");

Here, 3 is being auto-boxed and hence i and j point to the same Integer.

Integer i=3;
Integer j=new Integer(3);
if(i==j)System.out.println("i==j"); // does not print

Here, i points to the auto-boxed Integer whereas j points to a new Integer and hence there references fail the equals == operator test.

But, here's some more food for thought.

Integer i=300;
Integer j=300;
if(i!=j)System.out.println("i!=j"); // prints i!=j

Why? Because, auto-boxing shares Integer instances between -128 to 127 only. This behaviour, however, may differ between different Java implementations.

like image 24
Ravi K Thapliyal Avatar answered Nov 15 '22 17:11

Ravi K Thapliyal


Java pools integers between -128 and 127 and hence both the references are the same.

Integer i=3;
Integer j=3;

This results in autoboxing and 3 is converted to Integer 3. So for i is referring to an Integer object that is in constant pool, now when you do j=3, the same reference as that of i is assigned to j.

Whereas below code:

Integer j=new Integer(3);

always results in a new Integer creation, in heap. This is not pooled. And hence you see that both reference are referring to different objects. Which results in

Integer i=3;
Integer j=new Integer(3);
if(i==j)
   System.out.println("i==j"); // **does not print**
like image 23
zerocool Avatar answered Nov 15 '22 16:11

zerocool