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?

How to construct objects with new operator, How to Construct objects with n...

How to Construct objects with new operator? class Car { String licensePlate; // e.g. "New York 543 A23" double speed; // in kilometers per hour double maxSpeed;

Intro to Programming, Overall Requirements Every phone number is broken up ...

Overall Requirements Every phone number is broken up into sections as shown below: Country Code Area Code Prefix Line Number 1 919 882 5000 Write a program to separate out a ph

A java gui application, Design A Word document/ report containing pro...

Design A Word document/ report containing program design documentation, including UML diagrams (Use case, class and sequence) and written descriptions of each class or module

#tSutherland cohen line clipping algorithm, how do you write the code so th...

how do you write the code so the line will be clipped

Linked list, Write a program that takes as input a sequence of transactions...

Write a program that takes as input a sequence of transactions of the form "buy x; share(s) at $y each" or "sell x share(s) at $y each," assuming that the transactions occur on con

How can you performance tune your database?, 1. De normalizes your tables w...

1. De normalizes your tables where required. 2. Proper use of index columns: An index based on numeric parts is more efficient than an index based on character columns. 3. Re

What do you mean by bean wiring, The act of making associations between app...

The act of making associations between application components (beans) within the Spring container is reffered to as Bean wiring.

Prepare a computer graded test, Please check out the given instruction that...

Please check out the given instruction that I received to do the assessment. I can provide you that link to go on and submit the answers. To assess your coding skills, we would

Explain vector or object-oriented graphics, Explain Vector or Object-Orient...

Explain Vector or Object-Oriented Graphics? The representation of graphical objects, like as lines, circles, arcs, and rectangles, along with mathematical formulas. This techni

What is a java applet, Question 1: (a) What are the disadvantages of u...

Question 1: (a) What are the disadvantages of using HTML Frames in your web pages? (b) Give five examples of controls used inside HTML forms and the situation in which the

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