• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question

 
srini bhav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer but do you know about this teststring method ?
 
Jyotsna Clarkin
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jyotsna .
This was really driving me crazy.
I thought that was a method defined in the String class.
Srini
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic