Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() method comparing String and StringBuffer objects.

 
Nitasha Gupta
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I found this piece of code in mock exams for SCJP5.0

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");
}
}
}
Answer: No ouput

My Question is
Why isn't there any output when the strings are compared?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
API: String.equals:
Compares this string to the specified object. 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.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitasha Gupta:
Why isn't there any output when the strings are compared?


Because you're not comparing Strings, you're comparing a String to a StringBuffer.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the String.equals() API spec:

"Compares this string to the specified object. 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."



Since StringBuffer is like a String but not a String the result must be false.

From reading Object.equals(), which StringBuffer does not override(glad all my suffering in learning equivelence relations is not in vain). it looks like if that line were the converse it would also return false.
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Nitasha Gupta,

The String's equals method take the Object as arguments, since it overriding the Object's equals method. String can't implement equals method as String as arguments. So the String's equals method forced to take Object as arguments.

1. Inside the String's equals method, first check for == between method's instance and instance which is passed. If its statisfied means, equals method returns true.

2. Second, the method check instanceof operator for the object passed as argument to String. If the passed argument is String, then only it carries the character checking. If all characters are equal in both object, then return true otherwise false.

From the above point, i think you know the reason, why its returns false.

Any clarification needed, i will help you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic