• 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

Are Wrapper values Immutable ?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
chand here, i have doubt in wrapper.As it says Wrappers's values in wrapper class are immutable(can't modify once if assign).
can any one explain me this...

double d = 10.00;
Double wd = new Double(d);
wd++;
System.out.println(wd);

the output is: 11.00
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I heard too. But immutable doesn't mean it can't pretend to be mutable. Just like string, there are probably process done in the background that you are not aware of. Such as a new object is created automatically with the newly updated value. I believe that is happening in this case. So you have to watch out both ways depending what the question is asking.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrappers are immutable. This was done so that you can use them in more than one context and not worry about them being changed.

With Java 5.0, there is now auto-boxing and auto-unboxing so that the increment operators now work on wrappers. What happens is that the wrapper is converted to a primitive, the incrementation is done and then the result is a new wrapper object.

My opinion is that I'd rather box and unbox myself but that's the way that it is now with Java 5+

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

Its the object, here, that is immutable, not the reference. wd++ will internally create a new wrapper Object and assign it to wd.

Correct me, If i am Wrong.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class wrappers
{
public static void main(String args[])
{
double d = 10.00;
Double wd = new Double(d);
wd++;
System.out.println(wd);

}
}
output:
D:\SCJP 1.4\wrappers.java:7: operator ++ cannot be applied to java.lang.Double
wd++;
^
1 error

just analyze it...
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you are getting compiler error because autoboxing doesnt work under JDK 1.4 or ealier you must use JDK 1.5 or later.

consider this code and notice how wrapper objects are immutable:


here is wat happen in line #1, the object refered by d1 is first unboxed then incremented then reboxed after that a newly created object is assigned to d1, hence would return false comparing to d2.
 
reply
    Bookmark Topic Watch Topic
  • New Topic