aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

usha prithvi
Ranch Hand

Joined: Jun 22, 2005
Posts: 31
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

Joined: Aug 03, 2002
Posts: 7729
Please use tags, it makes it easier to read your code.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
narendra darlanka
Ranch Hand

Joined: Jun 17, 2005
Posts: 66
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


~naren<br /> scjp1.4<br /> scwcd1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Take a look at StringBuffer's API. Look for the equals method...
Chris Downey
Greenhorn

Joined: Jul 23, 2005
Posts: 4
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: StringBuffer