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

StringBuffer

 
usha prithvi
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain why doesn't the code reach in the if block

Thankz
[ July 26, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use tags, it makes it easier to read your code.
 
narendra darlanka
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
the StringBuffer class does not override the default equals() methods.so it checks for equality of references,which are always different for newly created object. so ur if condition just checks whether sb1==sb2 which is false.hope this helps
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at StringBuffer's API. Look for the equals method...
 
Chris Downey
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to spoil your learning opportunity, so allow me to suggest a hint:

(a) change the "if" test and see what happens:
if(sb1.toString().equals(sb2.toString()))

(b) check the API docs to understand the difference between String and StringBuffer in regards to equals()

Good luck!

Chris
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic