• 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

Dumb question on the wrapper classes

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been spending most all of my time in the K&B on the studying "run" towards the test. I happened to drift into another book just te get another perspective and I SWEAR that it said that "wrapper classes were immutable"..... that once they were assigned a value, that was IT!

Well, I just blew a question on a quiz because I said that an Integer reference (call it Integer x ) could NOT be incremented. I said that



would NOT compile.
Yet it did. I went and prototyped some tests and you can add TO Integers, you can increment.....

so my questions boil down to this:
a) are Wrapper class objects immutable?
b) if they ARE.... are these things working like Strings, where we build a new string, stuff it in the reference and LOSE the old String?

What am I not seeing right...?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so my questions boil down to this:
a) are Wrapper class objects immutable?



Yes.

b) if they ARE.... are these things working like Strings, where we build a new string, stuff it in the reference and LOSE the old String?



Well, kinda. The autoboxing mechanism will unbox the value, do the increment, and box the result. The boxing of the result should have the reference assigned to a different object.

Henry
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it leaves behind "islands of isolation" ?
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vyas Sanzgiri:
So it leaves behind "islands of isolation" ?




How do you reason that there will be islands (multiple objects that refer to each other that are no longer reachable)?
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry! That makes sense.
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a question!
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrapper objects are immutable. Once they have been given a value, the value cannot be changed.

Integer x = 32; //Make a wrapper object
x++; //Compiler does the auto-unboxing and reassignment.

It creates a second wrapper object, assigns it value of 33 and the makes x refer to the new object. So even though the original object is immutable, a new object is created and the reference variable now points to the new object. The original object is unreachable.

Interger x = 5;
Integer y = x;
System.out.println(y==x);
y++;
System.out.println(x + " " + y);
System.out.println(y==x);

output: true
5 6
false

When the compiler gets to the line y++, it substitiues it like this:

int temp = y.intValue(); //unwrap
temp++; //use it
y = new Integer(temp); //rewrap
reply
    Bookmark Topic Watch Topic
  • New Topic