• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Immutable wrapper classes

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
public class amap
{public static void main(String arg[])
{
AbstractMap ap=new HashMap();
ap.put(new Integer("2"),null);//line1
System.out.println(ap.containsKey(new Integer("2")));//line 2
}
}
Line 1 above returns true. Does that mean new Integer("2") in line 2 points to the same object as in line 1. Does it prove that primitive wrapper classes are immutable.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sumit David,


Line 1 above returns true. Does that mean new Integer("2") in line 2 points to the same object as in line 1?


No. The object created at line 1 and the object created at line 2 are TWO different objects. Their references are not equal.
However the equals method of Integer returns true, because the *contents* of these two distinct objects is the same. Object.equals() compares references. Integer overrides equals(). Integer.equals() compares contents.
new Integer(�2�) == new Integer(�2�) is false.
new Integer(�2�).equals(new Integer(�2)) is true.


Does it prove that primitive wrapper classes are immutable?


containsKey returns true because the objects are equals() according to Integer.equals(). The Map interface specifies that containsKey uses the equals() method.
I am not sure how to prove the wrapper classes are immutable.
[ February 06, 2004: Message edited by: Marlene Miller ]
 
Sumit David
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
It cleared all my doubt. In fact, it just slipped from my mind that the keyword new creates a new object.
Thanks again Sumit
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't sure whether I had answered the question you were asking. Thank you for letting me know that your doubts are cleared.
Yes, you are right - new returns a different reference each time.
[ February 08, 2004: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are told that the wrapper classes are immutable. But how do we know that?
I looked at the 1.4 source code. The Integer class is final. So it cannot be extended.
The field that hold the integer value is private but Not final!
 
The moustache of a titan! The ad of a flea:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic