File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in understanding how are Wrapper classes immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Help in understanding how are Wrapper classes immutable" Watch "Help in understanding how are Wrapper classes immutable" New topic
Author

Help in understanding how are Wrapper classes immutable

Siphiwe Madi
Ranch Hand

Joined: Aug 16, 2007
Posts: 70
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


Regards, Siphiwe Madi
[SCJP, SCWCD, __] Next ... scjwsd
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35252
    
    7
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:


Android appsImageJ pluginsJava web charts
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
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

Joined: Aug 16, 2007
Posts: 70
Thanks a lot for you help - now I understand.

Regards,
Siphiwe Madi
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Help in understanding how are Wrapper classes immutable
 
Similar Threads
few queries
Wrapper Class Object Doubt
Are Wrapper Classes always Immutable
Doubt on Marcus Green's Mock Answers
premitive types & Wrapper clases