• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

StringBuffer

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ObjectTest {

public static void main(String [] args ){
StringBuffer sb1 = new StringBuffer("ObjectTest");
StringBuffer sb2 = new StringBuffer("ObjectTest");

System.out.println(sb1.equals(sb2));
}
}

Why above code prints false when both strings are equal. When i replace StringBuffer with String, it would print true. Why String and StringBuffer behave differently,

Thanks,

Vidya
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer is not overriding equals from Object, thus here references are compared - not actual strings

here is some old topic on same subject:
https://coderanch.com/t/143540/Mock-Exam-Errata/certification/equals-StringBuffer

also found a "bug report" at sun.com :-)
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5082260


hope this helps..
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String and Wrapper classes override equals
whereas StringBuffer does not so if you invoke equals on StringBuffer it will return false always.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shyam kumarK said:


String and Wrapper classes override equals
whereas StringBuffer does not so if you invoke equals on StringBuffer it will return false always.



While kumarK is right for different StringBuffer objects the equals will return always false, it will return true though for different references that point to the same object.



just to make it clear for "always"

Cheers,
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes StringBuffer is a final class .. soo it cannot be overridden .. Hence we can't override equal method ..

That is the reason why false is printing ..
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread might help

https://coderanch.com/t/263456/java-programmer-SCJP/certification/EQUALS-method

Regards
Padma
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer does not override equals and hashCode methods so if you invoke equals on StringBuffer it will return false always.
 
get schwifty. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic