• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help in understanding how are Wrapper classes immutable

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I read on the Core Java Vol 1, that:
Wrapper classes are immutable - you cannot change a wrapped value after the wrapper has been constructed.

I understood this piece of information to imply that once you construct a wrapped object holding a value of (e.g 3),
then you cannot change that value to (e.g. 4).

code:
----
public static void main(String[] args) {

Integer i = 3;
System.out.println("initial value: " + i); // line 1: prints 3

//change the wrapped value
n++;
System.out.println("final value: " + i); // line 2: prints 4
}
----

I expected that the output of line 2, would print 3 (as it is said that you cannot change that value of a wrapped object), but instead the output is 4.

Can anyone please help me understand?

Regards,
Siphiwe Madi
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you meant "i++", not "n++".

The value of the Integer object didn't change. A new object got created and assigned to the object reference named i. The value of the original object is still the same, although its value isn't accessible any longer. If you create a second reference to the object, you can check that its value didn't change. Try the following:
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the object itself is immutable// prior to java 5, this won't work

however in java5 it unboxes the value stores in a temp var increment it and then auto-box it again.

So something like this:

Integer i = 3;

i++ //i'm assuming this is i not n as you wrote

would be automatically converted to:

temp_i = i.intValue();

temp_i++;

Integer i = new Integer(temp_i);

this is autoboxing
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for you help - now I understand.

Regards,
Siphiwe Madi
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic