Reputation: 43
why this program is printing false in first print statement and true in print statement.? i and i1 are two different objects so the first statement must print "true", which is as expected, but the second print statement prints "false" which creates a confusion.
public static void main(String[] args) {
Integer i = new Integer(10);
Integer i1 = new Integer(10);
System.out.println(i == i1); //printing false
i++;
i1++;
System.out.println(i == i1);//printing true
}
Upvotes: 0
Views: 2342
Reputation: 329
The Integer class has as many other classes implemented the equals method. You can easily compare two instances of Integer like this:
Integer int1 = new Integer(2);
Integer int2 = new Integer(2);
if(int2.equals(int1)) {
// true
}
Consider implementing the equals method in all your own entity/dtos:
class Car {
private String color;
private Integer model;
public Car(String color, Integer model) {
this.color = color;
this.model = model;
}
public String getColor() { return color; }
public Integer getModel() { return model; }
@Override
public boolean equals(final Object obj) {
if(this == obj) {
return true; // You are actually comparing the same instances
}
if(obj instanceof Car) { // Now you know both obj are instances of class Car
final Car other = (Car) obj; // Safe to cast
return Objects.equal(color, other.getColor())
&& Objects.equal(model, other.getModel()
;
}
return false;
}
}
Upvotes: -1
Reputation: 49986
Here:
Integer i = new Integer(10);
Integer i1 = new Integer(10);
you ask for dynamically allocated two instances of Integer class type, this will give you two different references. If you change it to:
Integer i = 10;
Integer i1 = 10;
then reference of i and i1 will be equal because for small values wrapper classes use cached objects.
This:
i++;
i1++;
is explained in: 15.14.2. Postfix Increment Operator ++ in JLS (emphasis mine):
Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) *and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.
also a note below is important which says that before operator is applied an unboxing conversion may happen:
Note that the binary numeric promotion mentioned above may include unboxing conversion (§5.1.8) and value set conversion (§5.1.13). If necessary, value set conversion is applied to the sum prior to its being stored in the variable. so it means it is boxed back to the wrapper type
so in the end after applying an operator ++ to either reference type of allocated Integer instance or a boxed Integer class, you end with a boxed Integer if its value is in the correct range for boxed types.
Upvotes: 1
Reputation: 1
first is reference comparison, hence getting false and second is the value comparison (while incrementing, it is un-boxed to primitive int and hence incremented to 11 and auto-boxed again; same for i1) . second call is (11 == 11) hence true.
Upvotes: 0
Reputation: 140318
Using the new
keyword always creates two different instances. So the following is always true:
new Integer(10) != new Integer(10)
hence the first line printing "false".
Then:
i++;
hides the unboxing and boxing. It is equivalent to:
i = Integer.valueOf(i.intValue() + 1);
As described in the Javadoc of Integer.valueOf
, values from -128 to 127 (at least) are cached: you are getting back the cached instance of Integer.valueOf(11)
for both i++
and i1++
, hence the second line printing "true".
Upvotes: 5