简体   繁体   中英

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.

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.

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.

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.

    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  

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.

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 .

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

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

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.

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