2

Have a look at this code

Integer x = 5;
Integer y = 2;
Integer xy = x+y;
System.out.println("xy = " + xy); // outputs: 7
System.out.println("xy2 = " + xy2); // outputs: 7
x++;
System.out.println("xy = " + xy); // outputs: 7
System.out.println("xy2 = " + xy2); // outputs: 7

How can I get the code to output 8 without using a method that calculates it for you?

asked Dec 21, 2013 at 20:08
11
  • 2
    Why in the world would you expect xy or xy2 to change when you change x? Commented Dec 21, 2013 at 20:10
  • 1
    @dasblinkenlight x and y are instances of Integer, xy is also a reference, xy2 is a value only. so technically, if I update x, xy should update as well because they are pointing to the same number Commented Dec 21, 2013 at 20:13
  • 1
    Your expectations are incorrect. Commented Dec 21, 2013 at 20:14
  • 2
    @Mazzy When you assign xy = x+y, a new object is created (or looked up in the interning cache if the result is small). The important thing, though, is that the third object is completely independent of the first two. Commented Dec 21, 2013 at 20:17
  • 1
    @Mazzy C++ and other languages have references to objects, not to expressions. Even if you change components of an expression through a reference, the value obtained through the expression before the change is not going to be recomputed. Commented Dec 21, 2013 at 20:23

4 Answers 4

5

An Integer in Java is immutable. You can not change its value. In addition, it's a special autoboxing type to provide an Object wrapper for the int primitive.

In your code, for example, x++ does not modify the Integer object x is referencing. It un-autoboxes it to a primitive int, post-increments it, re-autoboxes it returning a new Integer object and assigns that Integer to x.

Edit to add for completeness: Autoboxing is one of those special things in Java that can cause confusion. There's even more going on behind the scenes when talking about memory / objects. The Integer type also implements the flyweight pattern when autoboxing. Values from -128 to 127 are cached. You should always use the .equals() method when comparing Integer objects.

Integer x = 5;
Integer y = 5;
if (x == y) // == compares the *reference (pointer) value* not the contained int value 
{
 System.out.println("They point to the same object");
}
x = 500;
y = 500;
if (x != y)
{
 System.out.println("They don't point to the same object");
 if (x.equals(y)) // Compares the contained int value
 {
 System.out.println("But they have the same value!");
 }
} 

See: Why aren't Integers cached in Java? for more info (and of course the JLS)

answered Dec 21, 2013 at 20:14
Sign up to request clarification or add additional context in comments.

5 Comments

So it deletes the part in the memory, then does the increment and then assigns it to the same mem location?
No. x points to a completely different object in memory at the end.
Ok, so if I say: Integer x = 5; Integer xDuplicate = x; x++; xDuplicate--; then I'd have two completely different objects? x = 6 and xDuplicate is 4?
@Mazzy: Ignoring the pre-increment and post-increment difference (which doesn't matter here since the result of the expression is unused), x++; is the same as x = x + 1; So now can you see why it works the way it does?
That it's immutable is not really relevant.
3

You need to update xy after modifying x. So:

x++;
xy = x + y;
xy2 = x + y;

In Java you'll need to update a variable yourself if you want the value to change. It's not like other languages where you express a relationship between two variables and this relationship is maintained whenever a variable changes.

answered Dec 21, 2013 at 20:10

Comments

1

The expression: xy = x + y doesn't mean that now xy is depends on the values of x and y (if they changed, xy is changed too). You can see it as follows: the value of the expression x + y is inserted into xy.

Therefore, you must increase the value of x (by x++), before your set the value of xy.

answered Dec 21, 2013 at 20:17

Comments

-1

I'm new to java, and I'm not really sure of the context for this question, but if all you want to do is output 8, you can just make it xy++ instead of x++.

Integer x = 5;
Integer y = 2;
Integer xy = x+y;
int xy2 = x+y; // just testing to see if it makes a difference
System.out.println("xy = " + xy); // outputs: 7
System.out.println("xy2 = " + xy2); // outputs: 7
**xy++;**
System.out.println("xy = " + xy); // **outputs: 8**
System.out.println("xy2 = " + xy2); // outputs: 7
answered Dec 21, 2013 at 20:17

1 Comment

This is not the kind of answer he wants.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.