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
Describe Polymorphism? Polymorphism can be referred as one name many forms. It's the ability of methods to behave differently, depending upon object who is calling it. Key feat

How do I write a java program that enters a six digit number and takes the sum of the six digits?

can i have code on this assignment: The code in the main method should do the following: 1. Prompt the user for a string input value for the amount in dollars and store it in a va


what is function

1) Listing of the source code ( named Rainfall.java ) for your program 2) Two sample output/execution/runs of your program Optional Group Work: This assignment may be done in

When is AlreadyBoundException thrown and by which method? Ans) AlreadyBoundException is thrown by bind(String name) method when a remote object is already registered with the re

JZEBRA Integration into (Open ERP) Web Client Local Spooling/Printing to Local Print Device on ERP Client side Project Description: JZEBRA Integration into Open ERP Web Clien

What are the different scopes for Java variables? The scope of a Java variable is explained by the context in which the variable is declared. Therefore a java variable can have

i) Set the number of data points to 20 and the number of clusters to 2. Check box Show History. Click on Initialize. Click on Start. Click on Step . Observe which points ch