Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable Wrapper Class

 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Jackson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, you got it!

Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic