This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Question
 
Similar Threads
StringBuffer assigment
Working with Temporary Table
Garbage Collection
core java: can we compare two stringbuffer objects with equals() method
Working with Temporary Table