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?

Mazzy
  • 1,901
  • 2
  • 16
  • 36
  • 2
    Why in the world would you expect `xy` or `xy2` to change when you change `x`? – Sergey Kalinichenko Dec 21 '13 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 – Mazzy Dec 21 '13 at 20:13
  • Java is pass by value, always. See http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.htmland http://javadude.com/articles/passbyvalue.htm?repost – whaley Dec 21 '13 at 20:13
  • @whaley , that issue is a bad example as it is using int and not Integer ....... As I said, Integer is a class, int is not, big difference, if u used Integer in that function, it would work. – Mazzy Dec 21 '13 at 20:14
  • 1
    Your expectations are incorrect. – Sotirios Delimanolis Dec 21 '13 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. – Sergey Kalinichenko Dec 21 '13 at 20:17
  • @dasblinkenlight , that's the answer I was looking for, is this possible in for example c++ ? – Mazzy Dec 21 '13 at 20:18
  • 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. – Sergey Kalinichenko Dec 21 '13 at 20:23
  • Mazzi, x++ is short for “take the number from the Integer object that x is pointing at, increment it by one, wrap it into a new Integer object and then let x point at that new object“. As you can see, that does not contain the step “find all other pointers that point at a number that is a sum of x and some other number, increment all those by one as well and update all their references too“. Which is pretty good, think of the side effects that would have, as any number can be seen ad the sum of x and some other number. Basically, all numbers in your program would change! – Bernhard Dec 21 '13 at 20:26
  • @Mazzy it doesn't matter. You need to erase the expectation that Java has pass by reference anywhere, ever, and at all. Your question title seems to indicate that it does. – whaley Dec 22 '13 at 12:18
  • @whaley You're competely right, I just learned that java can pass by pointer value and not pass by reference at all, thanks ;) – Mazzy Dec 23 '13 at 01:11

4 Answers4

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)

Community
  • 1
  • 1
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • So it deletes the part in the memory, then does the increment and then assigns it to the same mem location? – Mazzy Dec 21 '13 at 20:23
  • 1
    No. `x` points to a completely different object in memory at the end. – Brian Roach Dec 21 '13 at 20:25
  • 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 Dec 21 '13 at 20:27
  • @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? – newacct Dec 22 '13 at 02:35
  • That it's immutable is not really relevant. – newacct Dec 22 '13 at 02:35
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.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
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.

Gari BN
  • 1,635
  • 2
  • 17
  • 31
-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
kidcuber
  • 51
  • 1
  • 4