Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

String buffer

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across following question in one of the mock exam.
StringBuffer sb1 = new StringBuffer("xyz");
StringBuffer sb2= new StringBuffer("xyz");
String ss1 = "xyz";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2)); // 2nd
System.out.println(sb1.equals(ss1)); //3rd
Answer is false, false, false.
Anybody would help me out why 2nd and 3rd comparison results in false ?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer does not override equals() method and hence returns false

Originally posted by Kalpesh Pandya:
I came across following question in one of the mock exam.
StringBuffer sb1 = new StringBuffer("xyz");
StringBuffer sb2= new StringBuffer("xyz");
String ss1 = "xyz";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2)); // 2nd
System.out.println(sb1.equals(ss1)); //3rd
Answer is false, false, false.
Anybody would help me out why 2nd and 3rd comparison results in false ?


 
Kalpesh Pandya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !! Its clear now.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalpesh'
There is no method like equals()in StringBuffer class.:rolleyes kidding).Hence it will always return false
Sandeep.
 
Those are the largest trousers in the world! Especially when next to this ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic