Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How equals method works in StringBuffer?

Reply
Thread Tools

How equals method works in StringBuffer?

 
 
swornavidhya_m@yahoo.co.in
Guest
Posts: n/a
 
      08-31-2006
Hai,
In my following code, the output i obtained is: false. Whereas
my expectation for output is true. I need ur suggestions and ideas.
The code is as follows:
Class stringBufferEqual
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Amit");
StringBuffer s2 = new StringBuffer("Amit");
System.out.println(s1.equals(s2));
}
}

Awaiting for ur suggestions and ideas in advance.


M.Sworna Vidhya

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      08-31-2006
<> wrote:
> Hai,
> In my following code, the output i obtained is: false. Whereas
> my expectation for output is true. I need ur suggestions and ideas.


StringBuffer's equals method returns true only when a StringBuffer
object is compared with itself. It returns false when compared with any
other StringBuffer, even if the two contain the same characters. This
is actually quite a sensible behavior. However, the meaning of equals
is unfortunately not defined very well, and it is inconsistently used
across the Java API, so this is a bit confusing.

To compare the String objects that are produced by the StringBuffer
objects in their current state, use s1.toString().equals(s2.toString())
instead.

--
Chris Smith
 
Reply With Quote
 
 
 
 
Jeffrey Schwab
Guest
Posts: n/a
 
      08-31-2006
Chris Smith wrote:
> <> wrote:
>> Hai,
>> In my following code, the output i obtained is: false. Whereas
>> my expectation for output is true. I need ur suggestions and ideas.

>
> StringBuffer's equals method returns true only when a StringBuffer
> object is compared with itself. It returns false when compared with any
> other StringBuffer, even if the two contain the same characters. This
> is actually quite a sensible behavior.


Chris, would you mind elaborating a little? I would have expected:

sb1.equal(sb2) == sb1.toString().equal(sb2.toString())
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-31-2006
Jeffrey Schwab wrote:
> Chris Smith wrote:
>> <> wrote:
>>> Hai,
>>> In my following code, the output i obtained is: false. Whereas
>>> my expectation for output is true. I need ur suggestions and ideas.

>>
>> StringBuffer's equals method returns true only when a StringBuffer
>> object is compared with itself. It returns false when compared with
>> any other StringBuffer, even if the two contain the same characters.
>> This is actually quite a sensible behavior.

>
> Chris, would you mind elaborating a little? I would have expected:
>
> sb1.equal(sb2) == sb1.toString().equal(sb2.toString())


The API documentation for StringBuffer lists "equals" as a method
inherited from Object, so I expect the same behavior as the Object
equals method.

Maybe it was done that way because you can always write
sb1.toString().equals(sb2.toString()) if that is what you mean.

Patricia


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      08-31-2006

"Jeffrey Schwab" <> wrote in message
news:lQAJg.1904$.. .
> Chris Smith wrote:
>> <> wrote:
>>> Hai,
>>> In my following code, the output i obtained is: false. Whereas
>>> my expectation for output is true. I need ur suggestions and ideas.

>>
>> StringBuffer's equals method returns true only when a StringBuffer object
>> is compared with itself. It returns false when compared with any other
>> StringBuffer, even if the two contain the same characters. This is
>> actually quite a sensible behavior.

>
> Chris, would you mind elaborating a little? I would have expected:
>
> sb1.equal(sb2) == sb1.toString().equal(sb2.toString())


StringBuffers use identity to determine equality, not contents. Strings
use contents to determine equality.

That is, two strings are equal if they have the same content. So code
like:

String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2));
System.out.println(str1 == str2);

should print "true" followed by "false", because it's true that their
contents are equal, but false that they are the same String.

StringBuffer did not override Object.equals(Object), and so the
implementation of equals in StringBuffer is essentially:

public boolean equals(Object other) {
return this == other;
}

- Oliver

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      09-01-2006
Oliver Wong wrote:
>
> "Jeffrey Schwab" <> wrote in message
> news:lQAJg.1904$.. .
>> Chris Smith wrote:
>>> <> wrote:
>>>> Hai,
>>>> In my following code, the output i obtained is: false. Whereas
>>>> my expectation for output is true. I need ur suggestions and ideas.
>>>
>>> StringBuffer's equals method returns true only when a StringBuffer
>>> object is compared with itself. It returns false when compared with
>>> any other StringBuffer, even if the two contain the same characters.
>>> This is actually quite a sensible behavior.

>>
>> Chris, would you mind elaborating a little? I would have expected:
>>
>> sb1.equals(sb2) == sb1.toString().equals(sb2.toString())

>
> StringBuffers use identity to determine equality, not contents.
> Strings use contents to determine equality.


Yes, but why was that decision made?
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      09-01-2006
Jeffrey Schwab <> wrote:
>
> Yes, but why was that decision made?
>


There are two approaches to the use of equals(Object). Unfortunately,
both are used from within the Java standard API.

The first approach says that equals(Object) should represent the concept
of two objects being the same. If a.equals(b) then it doesn't matter
whether I have a reference to 'a' or a reference to 'b'. This allows
equals(Object) to be overridden for String, Integer, Double, and any
other immutable value type. This approach would forbid implementing
equals(Object) to do comparison of contents for StringBuffer, because
even if two StringBuffer objects currently contain the same thing, I can
call append on one of them and it will be different from the other. Put
another way, it does matter whether I've got a reference to 'a' or 'b',
because someone might be modifying 'a'.

The second approach is that equals(Object) should be overridden to
implement whatever concept of equality seems most useful to the person
specifying a class or interface. In the standard API, this is seen for
java.util.List, which says that two lists are equal if they have the
same contents. This clearly violates the rule of the earlier approach,
since I can add elements to some Lists. The Collections API designer
just had a different idea about the proper use of equals(Object).

The first approach works better. For example, it is *not* safe to use
objects that have chosen this second approach as keys in a HashMap or
Hashtable. However, the second approach meshes better with some
people's intuition about what "equals" means, and it avoids the needs to
add other methods to do something like compare two Lists to see if they
have the same contents.

--
Chris Smith
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      09-01-2006
Chris Smith wrote:

> There are two approaches to the use of equals(Object). Unfortunately,
> both are used from within the Java standard API.
>
> The first approach says that equals(Object) should represent the concept
> of two objects being the same. If a.equals(b) then it doesn't matter
> whether I have a reference to 'a' or a reference to 'b'.
>
> The second approach is that equals(Object) should be overridden to
> implement whatever concept of equality seems most useful to the person
> specifying a class or interface.


Incidentally, I think the two approaches converge in this case. The name of
the class is StringBuffer (and later StringBuilder), not something like
MutableString. Instances have jobs to do, and that job is not representing
textual data, but knowing how to /assemble/ a String.

As such they are worker objects, and even when following the second approach
the "convenient equality" would tend to say that two instances were equal if
they were identically configured and were applying themselves to the same
target object. I can imagine builder objects for which those criteria would
allow a notion of equality that was weaker than identity, but I don't think
that's possible for StringBuilder.

(As an aside, I think the choice of toString() as the method to extract the
assembled String from the Builder was somewhat unfortunate since that isn't at
all what toString() /means/.)

-- chris


 
Reply With Quote
 
DJDaveMark DJDaveMark is offline
Junior Member
Join Date: Dec 2010
Posts: 1
 
      12-27-2010
Wath out for NullPointerException's!

Code:
StringBuffer sb1 = null;
StringBuffer sb2 = null;
boolean equals = String.valueOf(sb1).equals(String.valueOf(sb2));
System.out.println(equals);
Although it would be better to create your own Util method with something like:

Code:
public class EqualsUtil {
    public static boolean equals(StringBuffer sb1, StringBuffer sb2) {
        boolean equal = false;

        if(sb1 == null && sb2 == null) {
            equal = true;
        } else if(sb1 != null && sb2 != null) {
            equal = sb1.toString().equals(sb2.toString());
        }

        return equal;
    }
}
 
Reply With Quote
 
code learner code learner is offline
Junior Member
Join Date: Dec 2010
Posts: 19
 
      12-29-2010
Quote:
Originally Posted by DJDaveMark
Wath out for NullPointerException's!

Code:
StringBuffer sb1 = null;
StringBuffer sb2 = null;
boolean equals = String.valueOf(sb1).equals(String.valueOf(sb2));
System.out.println(equals);
Although it would be better to create your own Util method with something like:

Code:
public class EqualsUtil {
    public static boolean equals(StringBuffer sb1, StringBuffer sb2) {
        boolean equal = false;

        if(sb1 == null && sb2 == null) {
            equal = true;
        } else if(sb1 != null && sb2 != null) {
            equal = sb1.toString().equals(sb2.toString());
        }

        return equal;
    }
}

good advice DJDaveMark ...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
do I need to override the equals() method? Marteno Rodia Java 11 04-04-2009 06:12 PM
what is the use of capacity method in StringBuffer Sreenivas Java 26 01-18-2008 02:55 PM
TreeSet.contains and an OVERLOADED equals...works? LastHope Java 6 08-29-2007 07:01 PM
Replace method in stringbuffer chris1980 Java 12 01-18-2007 04:24 AM
StringBuffer equals method priyom Java 5 12-01-2006 10:19 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57