This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string buffer" Watch "string buffer" New topic
Author

string buffer

anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi guys,

code:
int a = 0;
StringBuffer s = new StringBuffer("Hello");
if ((s.length() < 10) | (s.append(" world").equals("Well done")))
; // do nothing
System.out.println("Value is - " + s);
in the above code, i thought the result is "Hello",b'cas string buffer doesn't override the equals method, so the result is same as s. but the ans is helloworld, how?
anybody correct me?
thx in advance


anushree
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
s.append(" world") modifies the content of the StringBuffer s.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313


s.length() IS less than 10 -- so that evalutates to true
then things get evaluated from left to right for the 2nd half of the equation:
s.append(" world") -- so at this point s is "Hello world"
s.equals("Well done") -- like you said the .equals method is not overridden in StringBuffer and this half of the equation evalutes to false
But... what would happen if the if statement looked like this:
if ((s.length() < 10) || (s.append(" world").equals("Well done")))


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: string buffer
 
Similar Threads
EQUALS method
equals method and StringBuffer
Again String.
StringBuffer and String doubt
please explain the output, comparing with String