• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

StringBuffer and String doubt

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are different Type of Objects

String -->Immutable
StringBuffer --> Mutable

So Incompatible DataType comes up.
No question of comparison comes into the picture.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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")..
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops.
 
Apoorv Srivastava
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for explaining pals..
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic