2
final Integer a = 1;
Integer b = a;

System.out.println("a: " + a); // prints 1
System.out.println("b: " + b); // prints 1

b++;
System.out.println("a: " + a); // prints 1
System.out.println("b: " + b); // prints 2

b = b + 1;
System.out.println("a: " + a); // prints 1
System.out.println("b: " + b); // prints 3

b = 10;
System.out.println("a: " + a); // prints 1
System.out.println("b: " + b); // prints 10

It would be great if somebody could explain the code output, expecially with relation to variable B.

4
  • Your output is wrong. After b++, b prints 2. Then, it prints 3, and finally 10. I just ran your code verbatim. Commented Jul 23, 2010 at 18:31
  • 1
    I modified your question to reflect actual output. Given that nothing strikes me as odd about the output, could you clarify what is confusing you? Commented Jul 23, 2010 at 18:33
  • I was using eclipses jpage/scrappage facility, hence the confusion that b++ didn't work like i would have wanted it to. Running it as a java class gave the result you state above and exactly the one I was expecting. I guess eclipse scrappage implementation has a bug. Commented Jul 23, 2010 at 18:35
  • In this case please try to add another print out after that b++ This may it be cause of that the value b was not evluated. Commented Jul 23, 2010 at 18:47

2 Answers 2

7

Ok, let's start with this:

final Integer a = 1;

You've created a final reference to an Integer object, which was autoboxed from a primitive int.

This reference can be assigned exactly once, and never again.

Integer b = a;

here you've created a second reference to the same object, but this reference is not final, so you can reassign it at your leisure.

b++;

This is a shorthand for the following statement:

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

And, coincidentally, the same for

b = b + 1;

The last statement:

b = 10

Is using autoboxing to shorthand this statement:

b = new Integer(10);

Hope this helps.

Sign up to request clarification or add additional context in comments.

9 Comments

I believe autoboxing uses Integer.valueOf, not new Integer.
You're probably right - I was more interested in the concepts than the specifics. In that it creates a new one (or returns from the internal cache) for the assignment.
Probably doesn't matter for this example, but I believe it uses Integer.valueOf(...) and not new Integer(...) so == still works.
But why if b++ is b = new Integer(b.intValue() +1) then print out print 1 ? This is standard post incremenation so the b have the value 2 in case after that sysout
@j flemm: Never, ever, ever count on that in real code. Just way too easy to make mistakes, even if the JLS specifies a minimum integer cache size. == only works if the value the Integer represents is within the pool of cached integers.
|
2

First of all, you must know that auto-boxing is happening here, you can read about that here.

Now only the b++ strikes me as non-straightforward. It is functionally equivalent to this code:

int bTemp = b.intValue();
bTemp++;
b = Integer.valueOf(bTemp);

Though the bytecode may be slightly different.

Comments

Your Answer

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.