Win a copy of Penetration Testing Basics this week in the Security forum!

# Doubt regarding StringBuffer

Mansukhdeep Thind
Ranch Hand
Posts: 1158
Hi all

Please have a look at the code below:

The output is : ab,bb

Regards
Mansukhdeep

Vinoth Kumar Kannan
Ranch Hand
Posts: 276
For this you need to know whether java passes values by-reference or by-value.
Take a look at this - http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Sit out with a pen and try to draw a diagram similar to Figure2 in that webpage. You'll find the answer yourself.

Mansukhdeep Thind
Ranch Hand
Posts: 1158
Vinoth Kumar Kannan wrote:For this you need to know whether java passes values by-reference or by-value.
Take a look at this - http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
Sit out with a pen and try to draw a diagram similar to Figure2 in that webpage. You'll find the answer yourself.

One more thing Vinoth. You are preparing for SCJP6 right. [Illegal Request DELETED]
Regards
Mansukhdeep

Shanky Sohar
Ranch Hand
Posts: 1051
Read kathy and sierra ..and mock exam can be found here on javaRanch
click on below linnk for mock exam.
http://faq.javaranch.com/java/ScjpMockTests

Shanky Sohar
Ranch Hand
Posts: 1051

sb1-----------a
sb2-----------b

after append(sb1,sb2) this will transfer the bits of sb1 and sb2
now
sb1 and sb2 recieves copies(bits representation) of main method sb1 and sb2

sb1.append(sb2)//will do the change in value in original as well as duplicates. "sb1" =ab but "sb2=b" only.

now sb1=sb2//will copy the bit pattern(for duplicates only) for sb2 to sb1...remember only bit pattern of sb1 got changed but for sb2 it will remain as it is.

sb1.append(sb2)//will do the changes for the value of sb1 and sb2 as the bit pattern for both is same.

so now sb1 and sb2 =bb.

Shanky Sohar
Ranch Hand
Posts: 1051
in terms of diagram we can have like this
so the
System.out.println("in append method");//insert this line will make it more clear
System.out.println(sb1 + "," + sb2);
prints bb,bb(because value for duplicates is getting printed pattern)

but in original it will prints sb1=ab and sb2=bb..

Shanky Sohar
Ranch Hand
Posts: 1051
i tried to make it more clear
Hope you understand it.
If not then let me knoe

Mansukhdeep Thind
Ranch Hand
Posts: 1158
Shanky Sohar wrote:i tried to make it more clear
Hope you understand it.
If not then let me knoe

Still confused Shanky. Tried another code. Please look at this:

It throws output:
Inside append method-->>abab,abab
abab,b

I understood the part inside he append method. But why is printing sb1,sb2 printing abab,b?

Regards
Mansukhdeep

Shanky Sohar
Ranch Hand
Posts: 1051
why not you try yourself by taking copy and pencil and make a above diagram..
once you make a diagram everything will be clear.

Shanky Sohar
Ranch Hand
Posts: 1051
public class StringBufferExample {
public static void main(String [] args){
StringBuffer sb1 = new StringBuffer("a");
StringBuffer sb2 = new StringBuffer("b");
append(sb1,sb2);
System.out.println(sb1 + "," + sb2);

}
public static void append(StringBuffer arg1, StringBuffer arg2){
arg1.append(arg2);
arg2 = arg1;
arg2.append(arg1);
System.out.println("Inside append method-->>"+ arg1 + "," + arg2);
}
}
okay,i will make a diagram again,

----------
- -
sb1--------- a -
- -
----------

----------
- -
sb2--------- b -
- -
----------

append(sb1,sb2);

----------
- -
sb1--------- a ---------arg1
- -
----------

----------
- -
sb2--------- b ----------arg2
- -
----------

arg1.append(arg2);//i assume you must know what it is doing

----------
- -
sb1--------- ab ---------arg1
- -
----------

----------
- -
sb2--------- b ----------arg2
- -
----------

arg2 = arg1;//this will copy the bit pattern of args1 to arg2.so now sb1,arg2 and arg1 all is having the same bit pattern

----------
- -
sb1--------- ab ---------arg1
arg2-------- -
----------

----------
- -
sb2--------- b -
- -
----------

arg2.append(arg1);//i assume you must know what it is doing

----------
- -
sb1--------- abab ---------arg1
arg2-------- -
----------

----------
- -
sb2--------- b -
- -
----------

so,
System.out.println("Inside append method-->>"+ arg1 + "," + arg2);
abab,abab

System.out.println(sb1 + "," + sb2);
abab,b

Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Shanky, I suppose Mansukhdeep Thind will understand it much better when he works it out, and directions would be enough. No offense.

Shanky Sohar
Ranch Hand
Posts: 1051
Vinoth Kumar Kannan wrote:Shanky, I suppose Mansukhdeep Thind will understand it much better when he works it out, and directions would be enough. No offense.

yes..i already told him to practice using copy and pencil....

Mansukhdeep Thind
Ranch Hand
Posts: 1158
Shanky Sohar wrote:
Vinoth Kumar Kannan wrote:Shanky, I suppose Mansukhdeep Thind will understand it much better when he works it out, and directions would be enough. No offense.

yes..i already told him to practice using copy and pencil....

Thanks Shanky. I did it myself and understood it.
Regards
Mansukhdeep

Shanky Sohar
Ranch Hand
Posts: 1051
Mansukhdeep Thind wrote:
Thanks Shanky. I did it myself and understood it.

Thats Great ..