File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic


usha prithvi
Ranch Hand

Joined: Jun 22, 2005
Posts: 31
Can someone explain why doesn't the code reach in the if block

[ 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
the StringBuffer class does not override the default equals() 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

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:

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

Good luck!

I agree. Here's the link:
subject: StringBuffer
It's not a secret anymore!