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?

Java graphic application, I need to draw cars and trucks and background wit...

I need to draw cars and trucks and background with road and a house with sky. I have to use Vehicle class as a parent class. To draw pictures, i should draw fun things coming up

Program for write a class with the header, PART1: Write a class with the...

PART1: Write a class with the header public class OrderedPair > implements Comparable > instance variables private Key x private Key y constructor publicOrd

Determine about the runtime security manager, Determine about the runtime S...

Determine about the runtime Security Manager Java also has a runtime Security Manager can check which operations a specific piece of code is allowed to do. Consequently, java c

Write complete application to play game tic-tac-toe, (Tic-Tac-Toe)  Create...

(Tic-Tac-Toe)  Create class TicTacToe that will enable you to write a complete application to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array o

Java assignment, Java Project Introduction: In this project, you will ex...

Java Project Introduction: In this project, you will explore a few sorting algorithms. You will also test their efficiency by both timing how long a given sorting operation take

Java web services, #quThis project is based on the teams example of chapter...

#quThis project is based on the teams example of chapter 1. Instead of teams, you will consider employees working in a department in a company. (Departments and employees are ana

In programming what is an object, In programming, what is an object? Ob...

In programming, what is an object? Object is a named collection of properties (data and state) & methods (instructions, behaviour). Objects are key to understanding object-orie

What is preferred organizational structure for organization, What is the pr...

What is the preferred organizational structure for the organization? Preferred Organizational Structure for organization is hierarchal. Where there is various departments inter

Advantage of event-delegation over event-inheritance model, Describe advant...

Describe advantage of the event-delegation model over the earlier event-inheritance model ?

What are commonsense guidelines to prevent viruses, What are Commonsense Gu...

What are Commonsense Guidelines to prevent viruses? Discuss about Antivirus as well. Commonsense Guidelines to avoid viruses are as follows: 1. Do not open attachment of unwant

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