wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explanation required for equals method in 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 "Explanation required for equals method in String class" Watch "Explanation required for equals method in String class" New topic
Author

Explanation required for equals method in String class

Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi All

I have come across a question

abstract class Lcc{
String Leader;
public void getLeader(){
}
}
public class Ilea extends Lcc{
public static void main(String argv[]){
Ilea i = new Ilea();
i.lrb();
}
public void lrb(){
String s1= new String("one");
StringBuffer sb = new StringBuffer("one");
if(s1.equals(sb)){
System.out.println("equals");
}
}

1. Compile time error, an abstract class cannot have non abstract methods.
2. Compile time error, the equals method of the String class takes a String parameter not a StringBuffer
3. Compile time error, class Ilea does not implement the getLeader method of Lcc
4. Compilation and output of "equals"
5.Compilation, but no output at runtime


Why the output is option 5? I expected it to print equals. After all both contains same sequence of characters.

Regards
Padma
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

It will compile but since there is no main function that calls public void lrb() it will not execute nor print


===Vyas Sanzgiri===
My Blog
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi

It is indeed there.

public static void main(String argv[]){
Ilea i = new Ilea();
i.lrb();

Regards
Padma
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343


From the API for String's equals method: "The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi Marc

I thing I got it. So it means that if it is given anything other than String Object (Like StringBuilder or StringBuffer objects) then it doesn't matter what it contains it will always return false. Then my point is why it accepts Object, it should have accepted String Objects only. I think it was done to override the equals method in Object class but I still tend to think that the equals method should have been wise enough to return true in the example.

Thanks
Padma
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Padma Asrani:
...but I still tend to think that the equals method should have been wise enough to return true in the example...

Well, the purpose of this equals method is to tell whether two Strings are equal, and it's wise enough to know when the other object is not a String. It's important to note that String, StringBuffer, and StringBuilder are each direct subclasses of Object. In other words, StringBuffer is not a subclass of String.

I understand what you're saying, but if the method returned true when given a StringBuffer or StringBuilder (or maybe even a char array?), then the argument might be, "Why is this telling me the objects are 'equal' when they're not even the same type?"

If you want to compare the "contents" of a StringBuffer or StringBuilder to a String, then simply obtain a String representation by calling toString on the StringBuffer, like this:

if(s1.equals(sb.toString()))...
[ June 18, 2007: Message edited by: marc weber ]
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi Marc,

Thanks for the explanation. I got it.

I was writing a code example using StringBuffer and StringBuilder classes. I was kind of puzzled to see that there is no equals method overridden in these classes and it seems to use the equals method of Object class. I wanted to compare two objects of type StringBuffer or StringBuilder then the only workaround is to convert those two objects to String using toString method and invoke equals method. It is cool. Isn't it?

Thanks
Padma
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Padma Asrani:
... I was writing a code example using StringBuffer and StringBuilder classes. I was kind of puzzled to see that there is no equals method overridden in these classes...

Instances of StringBuffer and StringBuilder are used to efficiently assemble Strings, and when they are done with their jobs, then the final product is produced by calling toString(). So you might regard instances of StringBuffer and StringBuilder as transitory "works in progress."
 
wood burning stoves
 
subject: Explanation required for equals method in String class