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
enthuware.ocajp.i.v7.2.1027
Moderator: admin
-
- Posts: 3
- Joined: Sat Jul 13, 2013 2:49 pm
- Contact:
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocajp.i.v7.2.1027
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Sat Sep 04, 2021 8:17 pm
- Contact:
Re: enthuware.ocajp.i.v7.2.1027
then when such operators, ++ and -- are used,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)
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?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocajp.i.v7.2.1027
Yes, when x is a reference to primitive wrapper. Not when x is a primitive.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 44 guests