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

Assignment Help:

 

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.

 


Related Discussions:- Difference between a stringbuffer and a string class?

Minimumshelf, Write a program that finds the minimum total number of shelve...

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process

Why is uml important? , The more complicated the underlying system, the mor...

The more complicated the underlying system, the more critical the inter communication among everyone involved in deploying and developing the software. UML is a software language f

What is constructor chaining, What is constructor chaining and how is it ac...

What is constructor chaining and how is it achieved in Java? A child object constructor always first requires to construct its parent (which in turn calls its parent constructo

How to creating arrays in java, How to Creating Arrays in java? Declari...

How to Creating Arrays in java? Declaring arrays merely says what type of values the array will hold. It does not form them. Java arrays are objects, and such as any other obje

Advantages java layout managers over traditional windowing, What are the ad...

What are the advantages that Java's layout managers provide over traditional windowing systems ?

Non-final functions in java object class, Normal 0 false fals...

Normal 0 false false false EN-US X-NONE X-NONE

Use the constructor, Can we use the constructor, instead of init(), to init...

Can we use the constructor, instead of init(), to initialize servlet? Ans) Yes. Of course you can use the constructor instead of init(). There's nothing to stop you. But you sho

Develop wechat program to integrate with sugarcrm, Wechat and SugarCRM P...

Wechat and SugarCRM Project Description: Seeking the partner able to develop wechat program to integrate with SugarCRM. Wechat Official Account able to perform some functi

What is jms administered object, A preconfigured JMS object (a resource man...

A preconfigured JMS object (a resource manager connection factory or a destination) formed by an administrator for the use of JMS clients and placed in a JNDI namespace

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd