This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String class???

 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lakshmi Grandhi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you can compare string variable with string literal like
a.equals("Sam");
I think this would help.
 
raymond yadao
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!!!
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget about the:
boolean equalsIgnoreCase(String anotherString);
method in the String class.
Brian
 
Alex Ku
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic