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.
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).......
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.
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.