• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equal() and ==

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" .
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your kind reference and I will buy the core J2EE - the 1st one.
Andrew
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following overrides equals() like you need:
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic