File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String buffer

 
Kalpesh Pandya
Greenhorn
Posts: 14
  • 0
  • 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 ?
 
mailssk
Greenhorn
Posts: 6
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !! Its clear now.
 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic