File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes equals() method comparing String and StringBuffer objects. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals() method comparing String and StringBuffer objects." Watch "equals() method comparing String and StringBuffer objects." New topic
Author

equals() method comparing String and StringBuffer objects.

Nitasha Gupta
Greenhorn

Joined: Jun 17, 2006
Posts: 14
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
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.


Entia non sunt multiplicanda praeter necessitatem
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
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


Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() method comparing String and StringBuffer objects.
 
Similar Threads
Abstract Class and methods
Explanation required for equals method in String class
OK, I know String's equal takes StringBuffer object, but when are they really equal?
Confusion in toString method os StringBuffer
Abstract Class