what is difference between integer a = 5 and new Integer(5)?
Asked Answered
C

5

1

if i write below code(in java):

Integer a =new Integer(5);
Integer b=new Integer(5);
if(a==b){
  System.out.println("In ==");
}
if(a.equals(b)){
 System.out.println("In equals");
}

My output is: "In equals" But if i change first and second line to ->

Integer a =5;
Integer b=5;

then my o/p is:

In == 
In equals

So what is difference in creating a Integer object? How it gets created when we do Integer a =5?

Does it mean that a and b object refer to same object, if i create Integer a=5 and creates another object Integer b=5 ?

Cowl answered 4/9, 2013 at 3:8 Comment(2)
You might want to tag this with the language you are using, as that will most likely make a difference.Vaccination
If I'm not wrong, that is javaSinuate
D
7

Integer a = 5; is called autoboxing, compiler converts this expression into actual

Integer a = Integer.valueOf(5);

For small numbers, by default -128 to 127, Integer.valueOf(int) does not create a new instance of Integer but returns a value from its cache. So here

Integer a = 5;
Integer b= 5;

a and b point to the same Object and a == b is true.

Drome answered 4/9, 2013 at 3:32 Comment(2)
Thanks for explanation, One more query i have if i do Integer b=2+3 then which operation will happen first (addition or autoboxing) ?Cowl
2 + 3 is a compile time constant expression, according to JLS compiler will replace it with Integer b = 5Drome
P
2

In Java, you should never use the new Integer, even though it is valid syntax it is not the best way to declare integers as you have found out. Use instead Integer.valueOf(int) This has multiple advantages.

You are not creating extra objects needlessly. Whenever you use the new operator you are forcing the vm to create a new object which is not needed in most cases. valueOf(int) will return a cached copy. Since Integer objects are immutable this works great. This means that you can use == though in practice you should use a null safe compare like in Apache ObjectUtils

The == operator tests for equality. References are only equal when they refer to the same object in memory. equals method ensures 2 object instances are 'equivalent' to each other.

Integer a = new Integer(5);
Integer b = a;

a == b; // true!
a.equals(b); // true
b = new Integer(5);
a == b; // false
a.equals(b); // true

Primitives are equal whenever their value is the same.

int a = 5; int b = a;
a == b;  // true!
Pina answered 4/9, 2013 at 3:31 Comment(0)
H
1

for primitive data types like int the equality operator will check if the variables are equal in value

for reference data types like your java.lang.Integer objects, the equality operator will check if the variables reference the same object. In the first case, you have two "new" and separate integer objects, so the references are different

Hairston answered 4/9, 2013 at 3:18 Comment(1)
How Integer object gets created when i do Integer a = 5 and next time if i do Integer b =5 then b refer to the same object a?Cowl
N
0

Integer wrapper shares few properties of String class. In that it is immutable and that can be leveraged by using intern() like functionality.

Analyse this:

String a = "foo";
String b = "foo";
String c = new String("foo");
String d = new String("foo");

a == b   //true
c == d   //false

The reason is when JVM creates a new String object implicitly it reuses existing String object which has the same value "foo", as in case a and b.

In your case JVM implicitly auto-boxes the ints and re-uses existing Integer object. Integer.valueOf() can be used explicitly to re-use existing objects if available.

Nautch answered 4/9, 2013 at 3:32 Comment(0)
P
0

I believe when you create using new operator it creates object reference. In first case, there are two object references and they are not same but their value is same. That is not the situation in second case.

Psychrometer answered 4/9, 2013 at 3:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.