This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
 
subject: Question
 
Similar Threads
Garbage Collection
Working with Temporary Table
StringBuffer assigment
Working with Temporary Table
core java: can we compare two stringbuffer objects with equals() method