what is the use of StringBuffer?

Answers were Sorted based on User's Feedback



what is the use of StringBuffer?..

Answer / koti reddy

A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:

x = "a" + 4 + "c"

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c")
.toString()

which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.
The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet".

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

Is This Answer Correct ?    6 Yes 0 No

what is the use of StringBuffer?..

Answer / guest

we can modify or change the value of string

Is This Answer Correct ?    6 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is the finalize method do?

0 Answers  


What string is utf8?

0 Answers  


Can we pass a primitive type by reference in java? How

0 Answers  


What is the difference between heap memory and stack memory?

0 Answers   Aspiring Minds,


There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which wants to have the properties of both LandAnimal and WaterAnimal. How will you design this situation?

6 Answers   KPIT,






What is encapsulation? Elaborate with example?

1 Answers   BMC,


What is java and their uses?

0 Answers  


What is the syntax and characteristics of a lambda expression? Explain

0 Answers  


Is null an object in java?

0 Answers  


What is Collections API?

1 Answers  


What is the java project architecture?

0 Answers   IBM,


What are latest features introduced with java 8?

0 Answers  


Categories