wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question" Watch "Question" New topic
Author

Question

srini bhav
Greenhorn

Joined: Feb 20, 2001
Posts: 12
Can anyone tell me the answer for this and explain it.
1. What is the output of this statement
StringBuffer sb = new StringBuffer("A");
StringBuffer sb2 = new StringBuffer("B");
testString(sb, sb2);
System.out.println("sb= " + sb + "\nsb2= " + sb2);
static void tellme(StringBuffer sb, StringBuffer sb2)
{
sb2.append("A");
sb2 = sb;
}
Thanks,
Srini
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
The output is sb = A sb2 = BA. Remember that everything is passed by value in Java, so the statement sb2 = sb changes the object reference in the method, but not the actual StringBuffer object that it is pointing to.
srini bhav
Greenhorn

Joined: Feb 20, 2001
Posts: 12
Thanks for your answer but do you know about this teststring method ?
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
There appears to be an error naming the methods in this code
since the parameter lists are the same their names should be identical. If you change the name of one to match the other the code will compile and give the output
Originally posted by srini bhav:
testString(sb, sb2);
static void tellme(StringBuffer sb, StringBuffer sb2)
{
sb2.append("A");
sb2 = sb;
}

srini bhav
Greenhorn

Joined: Feb 20, 2001
Posts: 12
Thanks Jyotsna .
This was really driving me crazy.
I thought that was a method defined in the String class.
Srini
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question