Page 1 of 1

enthuware.ocajp.i.v7.2.1027

Posted: Mon Aug 12, 2013 6:42 am
by Antenne88
Hi,

I don't understand this explanation.

1. Where has x been assigned a new Integer object with the value of 6?
2. Why does x have the value of 5 when it's printed and the explanation tells me it's containing 6?

Wrapper objects are always immutable. Therefore, when dataWrapper is passed into wiggler() method, it is never changed even when x++; is executed. However, x, which was pointing to the same object as dataWrapper, is assigned a new Integer object (different from dataWrapper) containing 6.

Any help would be very much appreciated :roll:

Re: enthuware.ocajp.i.v7.2.1027

Posted: Mon Aug 12, 2013 7:51 am
by admin
1. When you do x++;
This is same as doing x = x + 1;
So the original object that was passed was not changed but a new object containing 6 is created and assigned back to x. 'x' is a reference. x changes but not the object that was originally pointed to by it.

2. Where do you see x printed as 5? It prints 6 for x.

HTH,
Paul.

Re: enthuware.ocajp.i.v7.2.1027

Posted: Sun Sep 19, 2021 3:53 pm
by qazwsx922
it is never changed even when x++; is executed. However, x, which was pointing to the same object as dataWrapper, is assigned a new Integer object (different from dataWrapper)
then when such operators, ++ and -- are used,
in this questions, x++
always new object is created and assign updated value to it and also point to the newly created object instead of original(old) one?

Re: enthuware.ocajp.i.v7.2.1027

Posted: Tue Sep 21, 2021 1:58 am
by admin
Yes, when x is a reference to primitive wrapper. Not when x is a primitive.