This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
So far as I've learnt, == is used to check the memory address difference while equals() is used to check the object content. So, I don't understand why the result would print != as both object contents are 100. Also, I would like to know is the wrapper : Interger x = new Interger(100) immutable? Thanks for help. Andrew [ January 08, 2002: Message edited by: Cindy Glass ]
Yes, Integer objects are immutable. Since you haven't overridden equals() from java.lang.Object, you get the default implementation, which means you get this behaviour (quoted from the API):
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
This is why == and .equals() are (currently) the same. You need to add an equals method specific to this object...
Oh, I almost forgot. Usually I make a reference to Effective Java by Joshua Bloch (link to the Javaranch Bunkhouse). I find it an invaluable book to keep nearby but it also has great coverage of the whole .equals() thing. (this is what I was refering to when I said "or similar, this is just an example"). To see how great it is, follow the link and read Peter Tran's review (then buy the book! ) Dave. [ January 03, 2002: Message edited by: David O'Meara ]
JavaRanch has a section in the BunkHouse just for J2EE and such stuff. Go read the reviews. If you use the link from there to Amazon JavaRanch gets a few pennies to pay for this "upgrade" .
"JavaRanch, where the deer and the Certified play" - David O'Meara
Andrew Parker
Ranch Hand
Joined: Nov 12, 2001
Posts: 178
posted
0
Thanks for your kind reference and I will buy the core J2EE - the 1st one. Andrew
James Childers
Greenhorn
Joined: Jan 07, 2002
Posts: 9
posted
0
The following overrides equals() like you need:
"The enemy is never the enemy in his own eyes." - Sun Tzu