This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion between == & equals( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion between == & equals( )" Watch "confusion between == & equals( )" New topic
Author

confusion between == & equals( )

sangita chakor
Greenhorn

Joined: Dec 11, 2000
Posts: 1
confusion between == & equals()
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
jQuery in Action, 2nd edition
 
subject: confusion between == & equals( )
 
Similar Threads
what is the difference between == and .equals() method????
Are there any Parsi guys on JR?
difference between equals() and == and compareTo()
Significant different between == and equals()?
loop trouble