Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confuse about StringBuffer insert method

 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the fellowing code result ababcdcd,and why it is abababcd?
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
Signature of insert Method Is As Follows
1).public synchronized StringBuffer insert(int offset,
Object obj)
2).public synchronized StringBuffer insert(int offset,
String str)
3).public synchronized StringBuffer insert(int offset,
char str[])
4).public StringBuffer insert(int offset,
boolean b)
5).public synchronized StringBuffer insert(int offset,
char c)
6).public StringBuffer insert(int offset, //overloaded for long,
int i) //float,double...too

################################################333
Agrah Upadhyay
3rd Year
B.Tech
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Aghra,
i dont think just telling someone the signatures of a method would help him solve his problem, or help him learning.
if you know why he has got the above mentioned result. then you are requested to please expalin the procedure.
it would be great help for everyone.

Thanx

Sandy
 
Ganesh Prakhya
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dx Wu,

I executed your programme I got the following output. I think this is what you expected.

"ababcdcd"

Regards,
Ganesh..
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganesh,
But i am getting the same result as of dx wu

abababcd

Please Explain

Sandy
 
Dao Kia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, i am newbie to this forum.

I just tried with sb.insert(1,sb)

The result is "aaaaabcd"
 
Jimmy Thomas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the output that I got: abababcd
Tested on Win2k - Java 5 version.

First of all we must remember that the StringBuffer ultimately uses a Char array to store the contents of the string. Since we're using the same buffer object to do the insert, we're modifying the contents of the underlying array inturn. This is what is actually happening...

StringBuffer sb = new StringBuffer("abcd");

The above line creates a Char array containing {'a','b','c','d'}

sb.insert(2,sb);

Now are trying to insert at index two of the above created array, the point to note is that both the source & destination is the same char array. Initially the array len=4, during copy this what happens..

element 1 --> {'a','b','a','c','d'}
element 2 --> {'a','b','a','b','c','d'}
element 3 --> {'a','b','a','b','a','c','d'}
element 4 --> {'a','b','a','b','a','b','c','d'}
[ September 16, 2005: Message edited by: Jimmy Thomas ]
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got the result as ababcdcd what you expected.
I am really confused as why everyone is getting a different answer, can some help us please



Thanks in Advance,
Sangeetha
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jimmy,
Can you explain the element 3 and element 4. i have a doubt as why it agian inserts the char a in element 3 and char b in element 4.
 
Jimmy Thomas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sangeeta,

I tried the same program on Linux (JDK 1.4.2) $ I got the same that you've pointed out ababcdcd . I suspect there's some change in Implementation between the two version of JDK or due to the platform difference.

Could you tell me on what platform/JDK did you test ???

Can you explain the element 3 and element 4. i have a doubt as why it agian inserts the char a in element 3 and char b in element 4.


The key is the array we're reading from & inserting to are the same. So the 3rd element will the element that's inserted in the first loop & similarly for the 4th element.
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jimmy,
i am using windows xp / jdk 1.3.1_15
 
Jimmy Thomas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There change in implementation between the JDKs (1.5 & 1.4), in JDK 5 the insert method call gets delegated to the insert() of AbstractStringBuilder class. This is not a public class & I couldn't find the docs for the same.

Prior to JDK 5 the insert method functionality was handled by the StringBuffer class itself.

I guess this is the cause for the different outputs that we're getting...
[ September 16, 2005: Message edited by: Jimmy Thomas ]
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jimmy,
i am using windows xp / jdk 1.3.1_15
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
i think if all of you will mention the jdk version, then it will be more clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic