Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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.
 
Grow a forest with seedballs and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic