The Artima Developer Community
Sponsored Link

Java Answers Forum
string and string buffer??

3 replies on 1 page. Most recent reply: Aug 26, 2002 4:23 PM by Singh M.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
senthil

Posts: 23
Nickname: rickk84
Registered: Aug, 2002

string and string buffer?? Posted: Aug 17, 2002 8:46 AM
Reply to this message Reply
Advertisement
well what the difference between string and a string buffer...well am askin about that memory management between string and string buffer..and the reference they take when they are modified..


Javed

Posts: 15
Nickname: javedm
Registered: Aug, 2002

Re: string and string buffer?? Posted: Aug 19, 2002 2:01 AM
Reply to this message Reply
Hi Senthi,

Let us say we wanted to concat 3 strings s1,s2,s3

String s = s1+s2+s3;

What happens internally is a NEW StringBuffer is created, s1 and s2 are appened to it and is returned as a new String, This prosses iterates for all the strings u have given.

This is a big performence overhead.

Instead what we can do is creat a single instance of StringBuffer and append all the strings.

StringBuffer sb= new StringBuffer();
sb.append(s1);
sb.append(s2);
sb.append(s3);

return sb.toString().
This will improve performence a lot.

senthil

Posts: 23
Nickname: rickk84
Registered: Aug, 2002

Re: string and string buffer?? Posted: Aug 26, 2002 8:26 AM
Reply to this message Reply
well what happens now and whats the difference..
String s1 = "Java"+"Applet".
StringBuffer sb ="Java"+"Applet".

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: string and string buffer?? Posted: Aug 26, 2002 4:23 PM
Reply to this message Reply
Senthil, please read the java documentation of String and StringBuffer. That will help clarify these small things.

Anyway, from the javadoc of StringBuffer...

String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:


x = "a" + 4 + "c"
is compiled to the equivalent of:


x = new StringBuffer().append("a").append(4).append("c")
.toString()
which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.

Flat View: This topic has 3 replies on 1 page
Topic: Can anyone help me with this problem?? Previous Topic   Next Topic Topic: Getting the starting date of the week, if the week number is given

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use