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
Project Description: Overview I am seeking an Oracle ADF mobile app and web service to be created that takes a list of contacts stored on an Android mobile phone or tablet an

Project Description: I'm using a php script called phpfox on my site its work as social network . So I need is to use this script as CMS for my application. What users wil

The confidentiality and privacy may be accomplished through encryption. Encryption may be: Private-key or symmetric: This is based on a single key. This needs the sender and the

how i made a calculator in java plz tell help me


Explain the term- Comparing Strings For comparing Strings never use == operator, use equals method of String class. - == operator compares addresses (shallow comparison) whi

Session Bean : is a non-persistent object that provides some business logic running on the server. Session beans do not provide system break down. There are two kinds of session b

Write a program that prompts for two people’s birthdays (month and day), along with today’s month and day. The program should figure out how many days remain until each user’s birt

When should I use abstract classes and when should I use interfaces?  Use Interface, when: Design changing often or when various implementations only share method sign

What is the Comparable interface? Comparable interface is used to sort arrays and collections of objects using collections.sort() and java.utils. Objects of the class implement