File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes use of equals() method for SCJP 1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "use of equals() method for SCJP 1.4" Watch "use of equals() method for SCJP 1.4" New topic
Author

use of equals() method for SCJP 1.4

Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
Hi all... I am bit confused on equals method I thought it takes only variables with object references...
Integer L = new Integer(5);
Integer K = new Integer(5);
if ( L.equals(K)){ // This works fine since both are objects....
String H = "Hello";
if (H.equals("Hello"){ //This works fine too...??
Shouldnt the second if statement fail since it is not using any objects in equals method input param. I am confused.


SCJP 1.4<br />SCWCD 1.3
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well, 2 points needs to be cleared here.
1- Since all classes extends implicitly or explicitly, directly or indirectly the Object class, they all inherit the equals() method, and either they override it (like String) or just use it (like StringBuffer).
2- "any string" creats an object of type String and returns a reference.
So S1.equals("hello") is actually comparing 2 Strings one with a reference called S1 and the other has a reference with no name. it uses the String class implementation of the equals() method which also accepts objects of type Object, thus it returns true.
HTH
[ October 15, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please read the following article which provides extensive information about how to use the equals() method:
Equals And Hash Code by Manish Hatwalne


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
jQuery in Action, 2nd edition
 
subject: use of equals() method for SCJP 1.4
 
Similar Threads
How to over ride the List.contains() method.
Can someone help me with this
regarding scjp 1.5
vector contains()
Autoboxing & API :-D