What's the difference between the integer a = 5 and the 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 conclusion: "On equal terms" But if I change the first and second lines to →

Integer a =5;
Integer b=5;

      

then my o / p:

In == 
In equals

      

So what's the difference in creating an Integer object? How is it created when we do Integer a =5

?

Does this mean that objects a and b refer to the same object if I create Integer a=5

and create another object Integer b=5

?

0


source to share


5 answers


Integer a = 5

; called autoboxing, the compiler will convert this expression to the actual

Integer a = Integer.valueOf(5);

      

For small numbers, the default is -128 to 127 Integer.valueOf (int) does not create a new Integer instance, 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

- true

.

+4


source


In Java, you should never use a new Integer, even if it is a valid syntax, this is not the best way to declare integers as you figured out. Use Integer.valueOf (int) instead. This has several advantages.

You don't create unnecessary objects. Whenever you use a new operator, you are forcing 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 you can use ==, although in practice you should use a null safe comparison, for example in Apache ObjectUtils

The == operator tests for equality. References are only equal if they refer to the same object in memory. The equals method guarantees that two instances of an object 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 when their value is the same.

int a = 5; int b = a;
a == b;  // true!

      

+1


source


for primitive data types such as int

the equality operator checks if the variables are equal in value

for reference data types such as your objects java.lang.Integer

, the equality operator checks if the variables refer to the same object. In the first case you have two "new" and separate integer objects, so the references are different

0


source


The Integer wrapper has several properties of the String class. In this it is immutable and can be used with the intern () function, similar to the function.

Analyze 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 that the JVM creates a new String object implicitly, it reuses the existing String object which has the same "foo" value as in the case of a and b.

In your case, the JVM is implicitly auto-boxes int and reuses the existing Integer object. Integer.valueOf()

can be used explicitly to reuse existing objects if available.

0


source


I believe that when a new statement is created, it creates an object reference. In the first case, there are two object references and they are not the same, but their meaning is the same. This is not the situation in the second case.

0


source







All Articles