File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confuse about StringBuffer insert method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confuse about StringBuffer insert method" Watch "confuse about StringBuffer insert method" New topic
Author

confuse about StringBuffer insert method

dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60
I think the fellowing code result ababcdcd,and why it is abababcd?


good good study , day day up!
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
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

Joined: Aug 28, 2005
Posts: 340
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


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Ganesh Prakhya
Ranch Hand

Joined: May 05, 2004
Posts: 76
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

Joined: Aug 28, 2005
Posts: 340
Hi Ganesh,
But i am getting the same result as of dx wu

abababcd

Please Explain

Sandy
Dao Kia
Greenhorn

Joined: Sep 16, 2005
Posts: 2
Hi everyone, i am newbie to this forum.

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

The result is "aaaaabcd"
Jimmy Thomas
Greenhorn

Joined: Aug 17, 2005
Posts: 19
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

Joined: May 16, 2005
Posts: 23
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

Joined: May 16, 2005
Posts: 23
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

Joined: Aug 17, 2005
Posts: 19
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

Joined: May 16, 2005
Posts: 23
Hi Jimmy,
i am using windows xp / jdk 1.3.1_15
Jimmy Thomas
Greenhorn

Joined: Aug 17, 2005
Posts: 19
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

Joined: May 16, 2005
Posts: 23
Hi Jimmy,
i am using windows xp / jdk 1.3.1_15
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
Hi Friends,
i think if all of you will mention the jdk version, then it will be more clear.


Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confuse about StringBuffer insert method