This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes equals( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals( )" Watch "equals( )" New topic
Author

equals( )

Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
Hello All,

The following program prints "Both are not equal"... I cannot understand WHY? Please help.



public class T3
{
public static void main(String arg[])
{
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");
}
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Because String and StringBuffer are two completely different classes, and it's very bad practice for an implementation of equals() to return 'true' for instances of two different classes? I'm not sure why you think they should be the same.

Note that this evaluates to 'true':

if (str.equals(buffer.toString()))

i.e., if you convert the StringBuffer to a String, then the two are equal.


[Jess in Action][AskingGoodQuestions]
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

hi ernest,
I have a little question?

does buffer.toString() creates a new string object?
Or does it refer to the str in the string pool?
I was just wondering why does it return true when string str object is compared to buffer.toString()

Thanks


-kolli
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
It's all in the API documentation.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Note that the equals method is overridden in the String class but inherited in the StringBuffer class.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Originally posted by Roger Chung-Wee:
Note that the equals method is overridden in the String class but inherited in the StringBuffer class.
True. I have been caught out by that. If you look through the API documentation for StringBuilder you find "equals" in the block headed "inherited from Object" and in String you find the method in amongst all the other methods. That is how you can tell.

I enquired more about that, and found that Sun are of the opinion that something which changes all the time, like a StringBuilder (use StringBuilder instead of StringBuffer) ought not to have its own equals method. I am not convinced, but you can use
if(builder1.toString().equals(builder2.tostring()) . . .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Ravikanth kolli:
hi ernest,
I have a little question?

does buffer.toString() creates a new string object?
Or does it refer to the str in the string pool?
I was just wondering why does it return true when string str object is compared to buffer.toString()

Thanks


1) StringBuffer.toString() always returns a new String, each time you call it. It doesn't cache a reference to the original String you created it from, nor to any previous return value from toString().

2) The equals() method implemented by the String class returns true if the two Strings contain the same characters in the same order, even if they are two physically distinct String objects. If a class overrides equals(), that's generally why -- so that two "equivalent but distinct" objects can be considered equal.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
equals() must be the same regardless of how it's called

a.equals(b) ---=same as=--- b.equals(a)

this is the generally reason an instance of one class should not say it's equal to an instance of a different class - the other class might not agree,


(as stated before) if you want to know if they're the same string value use toString() - and don't worry about the memory usage.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
I Thank You All for yor precious help !!!
 
GeeCON Prague 2014
 
subject: equals( )