aspose file tools*
The moose likes Beginning Java and the fly likes Immutable Wrapper Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutable Wrapper Class" Watch "Immutable Wrapper Class" New topic
Author

Immutable Wrapper Class

Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
If Wrapper class is immutable .. why does this work?
public static void main(String args[]){
Integer I=new Integer(4);
I=new Integer(3);
System.out.println(I); //prints 3
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Jennifer Wallace:
If Wrapper class is immutable .. why does this work?
public static void main(String args[]){
Integer I=new Integer(4);
I=new Integer(3);
System.out.println(I); //prints 3
}


Well ask yourself this, can you write "I.changeValue(3)"?
No, you can't because there is no such method. You are creating a brand new object when you write
I = new Integer(3);
You are re-using the object reference you declared in the previous line. But you still have created two objects. The first one is "lost" on the heap, and is now eligible to be garbage collected.
One note, it's bad form to use an initial uppercase letter for your variable names.
use
Integer i = new Integer(4);
instead ;p

Rob


Rob
SCJP 1.4
Tony Jackson
Ranch Hand

Joined: Feb 23, 2001
Posts: 45
Two points:
1. (and I admit this is redundant but I want to add my spin): There's an important difference between an object and a reference to an object. When you say Integer myInteger; you are declaring a reference to an object, but the object doesn't exist. When you say Integer myInteger = new Integer(3); you are both creating an object AND a reference to an object, and the reference is TO the object created.
2. If you're going to make comments about coding style, you might as well be complete! Naming a variable i is bad form (some people argue it's ok for a for short and obvious loop counter). Variables, including object references should have names that reflect what they are. (Of course, since you are not using the variable, it's hard to know what to name it, but my point is to not encourage people to use "i" as a variable name.)
But you are absolutely right that whatever the name, it is important to avoid starting it with an uppercase letter, since that is the convention for Class names. So if you see something named Widget you know it's a class (or Interface) name but if you see something named widget you know its an object name.
Tony
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
I will keep the coding standards in mind.Thanks!
I am just sharing what I understand as of now.Do
correct me if I am wrong.
The following would be a proof that two different objects(and hence two different memory locations) are stored in the ref. variable. (Is there a better way to demonstrate it?)
public static void main(String args[]){
Integer var1=new Integer(1);
Integer var2=var1;
var1=new Integer(2);
System.out.println(var1 == var2); // prints false
}
In fact, this comparison would return false for any object, not just immutable classes, because of the "new" operator.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Yup, you got it!

Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Immutable Wrapper Class
 
Similar Threads
Number of Objects created
premitive types & Wrapper clases
Wrappers
Wrapper classes are immutable?
wrapper class