File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String class??? 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 "String class???" Watch "String class???" New topic

String class???

raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
Which methods can be legally applied to a string object?
this is one of the question from a mock exam and the answers are a,b,c,e.why is equals(String) can be applied to a string? i look at the javadoc(String class) i didn't find a method equals(String).......

thanks in advance!!!

Rob Ross

Joined: Jan 07, 2002
Posts: 2205
You're right, there isn't one. But due to how inheritance works, a String is an Object. That is, you can use a String any place an Object is used.
So calling equals() on a String with an Object OR a String is legal.
One side note, however, is that the equals() method of String overrides that of Object, and will first check and see if you are really passing in a String. If you're not, then there is no way to compare the two objects, so it has to return false. At runtime, passing an Object to equals will always result in false being returned.

SCJP 1.4
Lakshmi Grandhi

Joined: Jan 31, 2002
Posts: 5
Because you can compare string variable with string literal like
I think this would help.
raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
thanks guys!!!
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
And don't forget about the:
boolean equalsIgnoreCase(String anotherString);
method in the String class.
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Here is the source code from the java.lang.String
It tests if the Object is String or not.
All the source codes are in the src.jar file.
Open it with any zip program and you will find all
the source codes for Java classes.

I agree. Here's the link:
subject: String class???
It's not a secret anymore!