aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer and String doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer and String doubt" Watch "StringBuffer and String doubt" New topic
Author

StringBuffer and String doubt

akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70

is String and StringBuffer are incompatible types?
when we performs equals test on String and StringBuffer at line no 3 why they are not showing result sb equals s1.
euals does not mean meaningfully equivalent

We will keep things moving!!
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
the equals method of the StringBuffer class is not implemented so there is no way to campary a String object with a StringBuffer object, to compare those values you have to do yourString.equals(yourStringBuffer.toString());

also try testing two StringBuffers that holds the same string using the equals() method you will see that it will return false.

StringBuffer sb1 = new StringBuffer("123");
StringBuffer sb2 = new StringBuffer("123");
System.out.println("sb1.equals(sb2) --> " + sb1.equals(sb2));

the result will be ... false


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
is String and StringBuffer are incompatible types?


I think the compiler says that java.lang.StringBuffer and java.lang.String are incomparable (not incompatible) types because they are not part of the same class hierarchy, and for this reason there is no way for s and sb to reference the same object.


SCJP 5
Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
Both are different Type of Objects

String -->Immutable
StringBuffer --> Mutable

So Incompatible DataType comes up.
No question of comparison comes into the picture.


SCJP 1.6
SCWCD 1.5 (Preparing...)
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
HI,

At line 3, it will give compiler error as sb and s are of different types(StringBuffer and String respectively).
Operands of == operator should be type compatible.

Refer to JLS for more info!


And StringBuffer class doesnt override the equals() method. So when you use equals() method on StringBuffer object, it checks if both are referring to the same object or not. (not meaningful equivalence).

Hope this helps!


Thanks,<br />Srilatha M
Apoorv Srivastava
Ranch Hand

Joined: Jan 03, 2009
Posts: 43
omar al kababji wrote:the equals method of the StringBuffer class is not implemented so there is no way to campary a String object with a StringBuffer object, to compare those values you have to do yourString.equals(yourStringBuffer.toString());

also try testing two StringBuffers that holds the same string using the equals() method you will see that it will return false.

StringBuffer sb1 = new StringBuffer("123");
StringBuffer sb2 = new StringBuffer("123");
System.out.println("sb1.equals(sb2) --> " + sb1.equals(sb2));

the result will be ... false


Why is it so that when using StringBuffer equals() gives false even if they both have same data("123")..


SCJP 1.5...Preparing for SCWCD
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Apoorv Srivastava wrote:
Why is it so that when using StringBuffer equals() gives false even if they both have same data("123")..

Because StringBuffer doesn't override the equals() method defined in Object, which in turn uses == in its implementation. Therefore, when you use equals() with StringBuffer references what you are checking is if the references actually refer to the same object (not if the objects they refer to are meaningfully equivalent.)


All code in my posts, unless a source is explicitly mentioned, is my own.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
StringBuffer doesn't override the equals() method defined in Object, but actually inherits it. Therefore, when you use equals() on StringBuffer, it actually checks if the references actually refer to the same object
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
oops.
Apoorv Srivastava
Ranch Hand

Joined: Jan 03, 2009
Posts: 43
Thanks for explaining pals..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer and String doubt