aspose file tools*
The moose likes Beginning Java and the fly likes equal() and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equal() and ==" Watch "equal() and ==" New topic
Author

equal() and ==

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Here is my code:

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 ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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...

(or similar, this is just an example )
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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 ]
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
I think I will buy that 5-stars book. Thanks for your advice. Also, I want to learn the J2EE. Could you recommend one for beginner?
I have this one: http://www.amazon.com/exec/obidos/ASIN/0789725037/qid=1010120288/sr=1-6/ref=sr_1_11_6/002-1646055-8018453.
But, the chapter regarding RMI is hard to understand.
Thanks a lot.
Andrew
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
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
The following overrides equals() like you need:


"The enemy is never the enemy in his own eyes." - Sun Tzu
 
Consider Paul's rocket mass heater.
 
subject: equal() and ==