Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explanation required for equals method in String class

 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will compile but since there is no main function that calls public void lrb() it will not execute nor print
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

It is indeed there.

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

Regards
Padma
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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."
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic