Difference between a stringbuffer and a string class?, JAVA Programming

 

String is immutable in java: you can't change a string object but can replace it by creating a new object instance. Creating a new instance is rather expensive.

 

//Non efficient version using immutable String

 

String out = "Some text"

 

Int cou = 80;

for(int i =0; i

output += i;

}

return out;

 

The above code would build 79 new String objects, of which 78 would be thrown away

immediately. Creating new objects is not efficient.

 

StringBuffer is mutable in java: use StringBuilder or StringBuffer when you want to change the contents. StringBuilder was started in Java 5 and it is identical in all function respects to StringBuffer except that it is not synchronized, which gives it slightly faster at the cost of not being thread-safe.

 

// efficient version of using mutable StringBuffer

StringBuffer out = new StringBuffer(110);// set an stating size of 110

out.append(" text");

for(int i =0; i

out.append(i);

}

return out.toString();

 

The above code builds only two new objects, the final String and the StringBuffer that is given. StringBuffer expands as needed, which is costly however, so it could be better to initialize the StringBuffer with the needed size from the start as shown.

 

Posted Date: 7/25/2012 2:03:12 AM | Location : United States







Related Discussions:- Difference between a stringbuffer and a string class?, Assignment Help, Ask Question on Difference between a stringbuffer and a string class?, Get Answer, Expert's Help, Difference between a stringbuffer and a string class? Discussions

Write discussion on Difference between a stringbuffer and a string class?
Your posts are moderated
Related Questions
What is Bit-Mapped or Raster Graphics? Similar as object-oriented graphics, refers to software and hardware which use geometrical formulas to represent images. The other techni

Write a JavaScript program which having two functions that are sum and mean. The web page will ask the user to enter five values to be added. After taking input it will compute

list The Java Class Library? Java holds an extensive library of pre-written classes you can use in your programs. These classes are separated into groups known as packages. Th

The RMI architecture is based on one significant principle: the definition of behavior and the execution of that behavior are separate concepts. RMI allows the code that describes

The assignment is implementing a CLI in Java with several usable commands, using Threads.

iiiiii

how would I become expert in collection framework

Program for Randomly Generated 3- Digit Number Matches This assignment demonstrates your ability to use basic Java syntax including selection and looping statements. You will

hi,i need whatsapp panel to send unlimitd message. skype : maya123.com email :

Describe about the term Access Protection ? Global variables are a typical cause of bugs in most programming languages. A few unknown function can modify the value of a variabl