The Artima Developer Community
Sponsored Link

Java Answers Forum
StringBuffers

2 replies on 1 page. Most recent reply: Jan 9, 2003 5:11 AM by Arun

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 2 replies on 1 page
Amaka

Posts: 1
Nickname: maky
Registered: Jan, 2003

StringBuffers Posted: Jan 8, 2003 8:28 AM
Reply to this message Reply
Advertisement
What is the difference between Strings and StringBuffers


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: StringBuffers Posted: Jan 8, 2003 12:26 PM
Reply to this message Reply
A String is immutable, which means you cannot change it after it is created, whereas a StringBuffer is mutable and can be changed. If you want to build or modify a big string, it is easier and faster to use a StringBuffer. For general purpose use, such as passing parameters, Strings are better.

Note that when you do this:
   String s = "Cheese";
   s = s + "y";

You are not changing the string to which s originally refered, rather you are creating a new string and s now refers to that one.

With a StringBuffer, you could actually change it:
   StringBuffer s = new StringBuffer( "Cheese" );
   s.append("y");

Arun

Posts: 2
Nickname: kichu
Registered: Jan, 2003

Re: StringBuffers Posted: Jan 9, 2003 5:11 AM
Reply to this message Reply
Memory in Strings are created at compile time whereas in case of String Buffers they are created at runtime. So it is better efficient. Internally Strings call StringBuffers for manipulatiom.

Flat View: This topic has 2 replies on 1 page
Topic: OSX, Java, Packages. Previous Topic   Next Topic Topic: Splitting a String to a specific length

Sponsored Links



Google
  Web Artima.com   

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