Sathesh S
Sathesh S

Reputation: 1323

Incrementing and Decrementing changing object value

Can anyone tell me the reason for the change in output.

public class Demo {
  public void demo()
  {
        Integer y = 567;
        Integer x = y;
        System.out.println(x + " " + y);
        System.out.println(y == x);
        y++;
        System.out.println(x + " " + y);
        System.out.println(y == x);
        y--;
        System.out.println(x + " " + y);
        System.out.println(y == x);
  }
  public static void main(String args[])
  {
        Demo obj = new Demo();
        obj.demo();
  }
}

OUTPUT:

567 567

true

567 568

false

567 567

False

Here why i'm getting the final false.

Upvotes: 7

Views: 4225

Answers (8)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

    Integer y = 567; // y=567
    Integer x = y;   // x is equal to y object
    System.out.println(x + " " + y); // out put x and y so obviously x and y are 567
    System.out.println(y == x); // here x and y are in same reference. so this x==y is true and out put is true. 
    y++; // increment y by 1. then y=568
    System.out.println(x + " " + y); // now x= 567 and y= 568
    System.out.println(y == x);// now x not equals to y then false will print
    y--; // again decrement y value
    System.out.println(x + " " + y); // again x and y are same 567
    System.out.println(y == x);// here y.value == x.value but x and y object wise not equal since object x and y are referring deference points  

Upvotes: 1

JHS
JHS

Reputation: 7871

When you use primitive int instead of the Integer. The final output would be true.

However when you use Integer class, these are Objects. If you use the equals method the final output would be true.

Upvotes: 0

johnchen902
johnchen902

Reputation: 9599

y == x checks for content-equality, that is, whether they pointed to the same object, not whether the object they pointed to contains the same int. Especially when x, y >= 128.

Use

y.equals(x);

or

(int) y == (int) x

or declare x and y as int instead.

Note that auto-unboxing doesn't happen in Integer == Integer or Integer != Integer.

Upvotes: 0

Natalia
Natalia

Reputation: 4532

Even, if you create

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

a==b - false

but for

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

a==b - is true

In java there is a cache of small integers: -127 to 128. All other integers are newly created objects and they can not be equals.

Upvotes: -1

Buhake Sindi
Buhake Sindi

Reputation: 89169

This is because the compiler does this internally:

y--

means:

int _y = y.intValue();
_y--;
y = Integer.valueOf(_y);

Therefore, y is has a new Integer instance. You are doing Object reference check (when using ==) and not value equality check.

Use equals() method to evaluate 2 values.

Upvotes: 2

Uwe Plonus
Uwe Plonus

Reputation: 9954

You are using Integer which is an immutable object.

Basically your code is

y = new Integer(y.intValue() + 1);

and

y = new Integer(y.intValue() - 1);

Therefore you're creating two new Integer objects that are not the same (==) as the previous objects.

This behaviour is called autoboxing in Java.

Upvotes: 6

Marko Topolnik
Marko Topolnik

Reputation: 200158

Change your

    Integer y = 567;
    Integer x = y;

to

    int y = 567;
    int x = y;

and the suprprising behavior will be gone. My guess is that you have stumbled upon Java's implicit autoboxing of primitive values into wrapper objects, and are lead to believe that you are directly manipulating the numbers.

Upvotes: 3

ajay.patel
ajay.patel

Reputation: 1967

Because x and y are referring to 2 different objects.

y--;

This first unboxes y to int than decrements it and than boxes it to Integer. And this new boxed integer is referring to a different memory location.

Ideally, on wrapper classes, its better to call equals method on wrapper object rather than == operator.

Upvotes: 0

Related Questions