Results 1 to 5 of 5
Thread: Wrapper objects
- 12-14-2008, 08:25 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Wrapper objects
Hi every one,
"wrapper objects are immutable". This is what we learn in the theory. ok thats fine. but i am confuse with the concept of Autoboxing.
Integer i1=new Integer(10);
i1=20;
System.out.print(i1); // 20
If the wrapper objects are immutable how can we change the values of wrapper objects?????
Why it won't give compile error????
Please anybody help regarding this ..........
Thanks
- 12-14-2008, 08:37 PM #2
As a result of autoboxing, the statement
is executed asJava Code:i1 = 20;
whichJava Code:i1 = Integer.valueOf(20);
Returns a Integer instance representing the specified int value.
You're not changing the value of an Integer object, you're assigning a different object to be referenced by the variable.
db
-
I believe that the autoboxing here acts the same as when you assign a String to a new string literal: behind the scenes you are creating a new Integer object with your second line. In other words, I believe that this:
is really this:Java Code:i1 = 20;
[Edit: Too late! Beaten by Darryl despite his slow connection!]Java Code:i1 = new Integer(20);
- 12-14-2008, 08:47 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
you meant, if there are any changes(assignments, increment, decrement) to a wrapper object, it create new wrapper object with the new result and it change the same reference which was in the previous object to the new object??????
am i correct?????
- 12-14-2008, 08:48 PM #5
Boxed in
Probably because of the automatic box-unbox feature of Java. Read the following ... it explains it a little:
The Numbers Classes (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Luck,
CJSL
Edit: ooops... was a little slow in positng :-)Last edited by CJSLMAN; 12-14-2008 at 08:50 PM.
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Secure FTP Wrapper 3.0.3
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-23-2008, 01:56 PM -
Wrapper Class
By haiforhussain in forum New To JavaReplies: 6Last Post: 06-24-2008, 08:08 AM -
Secure FTP Wrapper 3.0.2
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-29-2008, 06:05 PM -
Java Service Wrapper 3.3.0
By Java Tip in forum Java SoftwareReplies: 0Last Post: 03-29-2008, 01:04 PM -
wrapper classes
By sireesha in forum New To JavaReplies: 5Last Post: 12-11-2007, 09:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks