• 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

confusion between == & equals( )

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
confusion between == & equals()
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you compare two objects in Java and you use the ==
such as a == b
Java will look at the ADDRESS that is in a and the ADDRESS that is in B and see if they are the same. This is very valid if you want to see if both a and b point to the exact same object. However if you want to see if the object pointed at by a and the object pointed at by b have 2 objects which are exactly ALIKE then you need to use equals().
Be carefull though because the Object class method "equals()" just uses the == comparison and not all classes override that method to privide meaningfull equals() methods.
(Note: the situation above also explains why there are clone() methods instead of just saying a = b which would just give you two variables containing the address of one object).
[This message has been edited by Cindy Glass (edited December 11, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic