简体   繁体   中英

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

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 ?

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 .

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!

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM