This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

Comparing String with StringBuffer

 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got caught with this question in a mock exam.
The code is:
------
String str = "Java";
StringBuffer buffer = new StringBuffer (str);
if (str.equals(buffer))
{
System.out.println ("Both are equal");
}
else
{
System.out.println ("Both are not equal");
}
------
The correct answer is "Both are not equal". but what I thaught was, since .equals() method always checks the contents of the objects, will pass the "if" condition.
can any one help me please?
Thnks & regards
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tausif,
The String class overrides the equals() method of the object class. The java doc for equals() method of String class says

public boolean equals(Object anObject) 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.

Hope this helps you
Reshma
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes it helps to go see the source code of J2SE classes. Below is the code for the equals method of the String class.
As you can see, the method will unconditionally return false if the type of the given argument is not of type String. This makes sense when you come to think about how two objects of different types could ever be equal.
The bottom line is: String and StringBuffer objects are always different as far as equals is concerned.
BUT, the condition would yield true if you would compare str with the content of the StringBuffer and not the StringBuffer itself. To get the content of the StringBuffer just invoke toString() on it.
str.equals(buffer.toString()) would yield true, and thus, satisfy the condition.

[ March 04, 2003: Message edited by: Valentin Crettaz ]
 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Valentin,
I m cool now
 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will produce the sentence: Both are equal
The method equals() is overridden. It will check if the object that buffer.toString() returns is an instanceof String. If this is the case, it will compare the two strings (buffer.toString and str).
My Study Notes
[ March 05, 2003: Message edited by: Karin Paola Illuminate ]
 
Jasper Vader
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i seem to remember that the StringBuffer does not overide the equals method.
so if u swapped it around so that the code read
if buffer.equals(str);
result could be true.
i should check this tho.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper,
Since StringBuffer does not override the equals method, there is no way that the result of
buffer.equals(str);
will ever be true, since a reference to a String object will always be different from a reference to a StringBuffer object.
Remember that the basic implementation of equals compares the references, that is, it performs a == comparison between the two arguments.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic