This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String class???" Watch "String class???" New topic
Author

String class???

raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
Which methods can be legally applied to a string object?
a.equals(String)
b.equals(Object)
c.trim()
d.round()
e.toString()
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!!!

raymond
Rob Ross
Bartender

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.


Rob
SCJP 1.4
Lakshmi Grandhi
Greenhorn

Joined: Jan 31, 2002
Posts: 5
Because you can compare string variable with string literal like
a.equals("Sam");
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.
Brian
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.

kawaii
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String class???