answersLogoWhite

0


Best Answer

In Java, String is a class that represents an immutable string, that is a sequence of characters that can never change. Any modification to the String will have to create a new String object. A StringBuffer (and in Java 1.5 and up, a StringBuilder) is a mutable string object that can be modified at runtime.

The advantages of using Strings are that the String is generally lighter and faster to use where the String isn't going to change. When building a long String of text by making changes to one object over time, you should use a StringBuilder or StringBuffer (in Java 1.4 or when synchronization is important).

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

In Java, String is the basic object used to represent a string of text. It is generally very fast. However, the String class is immutable - this means that a String object cannot be changed, once created; only new String objects can be created.

Because of the need to modify strings in place without the overhead of creating many new objects, the StringBuffer class was invented. A StringBuffer represents a string of text that can be changed in place. It has greater overhead than one String, but much less overhead than the many Strings that would be necessary to do what could be done with one StringBuffer.

The StringBuffer is thread-safe, meaning you can change a StringBuffer from multiple threads without worrying about race conditions. But this comes with a performance price, and isn't necessary in many cases. Therefore, Java 5 added the StringBuilder class, which is like the StringBuffer, but non-thread-safe. This means that it cannot be used from multiple threads at once, but are generally preferred in most cases where there is no danger of that.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

String class creates the strings of fixed length whereas stringBuffer class creates the strings of flexible length where we can modify the contents of a string both content and size.

The major differences between String and StringBuffer is how they are implemented inside Java, and how this implementation is meant to affect their use.

Most common operations can be performed on either a String or StringBuffer, but they will have very significantly different performance characteristics.

A String is implemented as an immutable object; that is, when you initially decide to put something into a String object, the JVM allocates a fixed-width array of exactly the size of your initial value. This is then treated as a constant inside the JVM, which allows for very significant performance savings in the case where the String's value is not changed. However, if you decide to change the String's contents in any way, what the JVM then essentially does is copy the contents of the original String into a temporary space, make your changes, then save those changes into a whole new memory array. Thus, making changes to a String's value after initialization is a fairly expensive operation.

StringBuffer, on the other hand, is implemented as a dynamically-growable array inside the JVM, which means that any change operation can occur on the existing memory location, with new memory allocated only as-needed. However, there is no opportunity for the JVM to make optimizations around the StringBuffer, since its contents are assumed to be changeable at any instance.

In short, you should use String where you intend to assign a value once and either never change it, or change it very seldom over the life of the program's run. In other cases, use StringBuffer.

In theory, neither String nor StringBuffer have limits on the max size of the string they can store. In practice, however, both work out to generally be about 2 million (16-bit Unicode) characters.

String class has a limit while there is no limit in StringBuffer class. In the StringBuffer class input is flushed while it is not so in the String class. String is an array whose all elements are of character type. String class is used in the same manner that we might use any other predefined class. For example we might declare an instance of the String class as follows: String name = new String();

Niraj

besides this there is one more difference----

between these two classes is performance where

* StringBuffer is faster than String when performing simple concatenations.

* When a String is being manipulated in code, character string are routinely

* concatenated like shown in the following example:

*

* String name = new String("Alex");

* name += ", Hunter";

*

* Now consider the same concatenation, but using a StringBuffer:

*

* StringBuffer name = new StringBuffer("Alex");

* name.append(", Hunter");

*

* Now to many developers, the examples above may seem very similar. The +

* operator appears innocent, but the code generated may produce some surprises.

* Using a StringBuffer for concatenation can in fact produce code that is

* significantly faster than using a String. To discover why this is the case,

* lets examine the generated bytecode from our two examples. The bytecode for

* the example using String looks like this:

*

* % javap -c t

*

* Compiled from t.java

* public class t extends java.lang.Object {

* public t();

* public static void main(java.lang.String[]);

* }

*

* Method t()

* 0 aload_0

* 1 invokespecial #1

* 4 return

*

* Method void main(java.lang.String[])

* 0 new #2

* 3 dup

* 4 ldc #3

* 6 invokespecial #4

* 9 astore_1

* 10 new #5

* 13 dup

* 14 invokespecial #6

* 17 aload_1

* 18 invokevirtual #7

* 21 ldc #8

* 23 invokevirtual #7

* 26 invokevirtual #9

* 29 astore_1

* 30 return

*

* The bytecode at locations 0 through 9 is executed for the first line of code,

* namely:

*

* String name = new String("Alex");

*

* Then, the bytecode at location 10 through 29 is executed for the

* concatenation:

*

* name += ", Hunter";

*

* Here is where things get interesting. The bytecode generated for the

* concatenation creates a StringBuffer object, then invokes its append method:

* the temporary StringBuffer object is created at location 10, and its append

* method is called at location 23. Because the String class is immutable, a

* StringBuffer must be used for concatenation.

*

* After the concatenation is performed on the StringBuffer object, it must be

* converted back into a String. This is done with the call to the toString

* method at location 26. This method creates a new String object from the

* temporary StringBuffer object. The creation of this temporary StringBuffer

* object and its subsequent conversion back into a String object are very

* expensive.

*

* In summary, the two lines of code above result in the creation of three

* objects:

*

* 1.) A String object at location 0

* 2.) A StringBuffer object at location 10

* 3.) A String object at location 26

*

* Now, let's look at the bytecode generated for the example using StringBuffer:

*

* % javap -c t

*

* Compiled from t.java

* public class t extends java.lang.Object {

* public t();

* public static void main(java.lang.String[]);

* }

*

* Method t()

* 0 aload_0

* 1 invokespecial #1

* 4 return

*

* Method void main(java.lang.String[])

* 0 new #2

* 3 dup

* 4 ldc #3

* 6 invokespecial #4

* 9 astore_1

* 10 aload_1

* 11 ldc #5

* 13 invokevirtual #6

* 16 pop

* 17 return

*

* The bytecode at locations 0 to 9 is executed for the first line of code:

*

* StringBuffer name = new StringBuffer("Alex");

*

* The bytecode at location 10 to 16 is then executed for the concatenation:

*

* name.append(", Hunter");

*

* Notice that, as is the case in the first example, this code invokes the

* append method of a StringBuffer object. Unlike the first example, however,

* there is no need to create a temporary StringBuffer and then convert it into

* a String object. This code creates only one object, the StringBuffer, at

* location 0.

*

* To summarize, StringBuffer concatenation is significantly faster than String

* concatenation. Obviously, StringBuffers should be used in this type of

* operation when possible.

yogeshkhanna88@gmail.com
This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The contents of a String can't be changed; if you do change the value, internally a new object is created.

A StringBuffer lets you modify its contents. Also, the StringBuffer class has some additional methods for string manipulation.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A String object in Java is an immutable object. If you wanted to concatenate Strings in Java:

String s = "S";

s += "t";

s += "r";

s += "i";

s += "n";

s += "g";

The end result here is that s = "String". The problem here is that you're changing the value of an immutable (unchangeable) type during each of the above lines. In order to compensate for this, you're basically creating a new String object on each line. This means getting rid of the old data, and completely reallocating memory for a new object. The kind folks at Sun decided to give us a StringBuilder class.

You can think of StringBuilder like this: a String is to a StringBuilder what a character array is to an ArrayList of characters. The StringBuilder will not suffer from poor performance in the way that a String would if you need to be constantly changing the data it contains.

StringBuilder sb = new StringBuilder();

sb.append('S');

sb.append('t');

sb.append('r');

sb.append('i');

sb.append('n');

sb.append('g');

By the end here, s and sb contain the same data, but the computer had to do a lot less work to put together the StringBuilder.

Note: The StringBuilder class is not thread-safe. If you're looking for a thread-safe version of StringBuilder, use the StringBuffer class.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An instance of string, once created, the content cannot be changed (immutable). StringBuilder, on the other hand, allows you to alter the content on the fly, such as insert, deleter, append, and truncate, all operated on the same instance.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Strings are extremely useful but at the same time resource intensive too. In programs where numerous strings are used which need to be altered every now and then, it is advisable to use the StringBuffer or the StringBuilder class.

The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make a lot of modifications to strings of characters. String objects are immutable, so if you choose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool. On the other hand, objects of type StringBuffer and StringBuilder can be modified over and over again without leaving behind a great list of discarded String objects.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Both gives same functionality. but StringBuffer is synchronized where as StringBuilder not

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

toString(), length() and equals() are common methods

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between String and String Buffer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between a buffer and an indicator?

Distinguish between buffer and indicator


What is the difference among string and string buffer and string builder?

String is the immutable class that means the object f that class never be changed. String is the Sequence of character.


What is the difference between a concentrated buffer and a diluted buffer?

The concentration.


What are the difference between buffer stock and safety stock?

no difference


What is the difference between n and endl in c plus plus?

The \n escape sequence simply inserts a newline within a string. std:endl does the same but also flushes the write buffer.


What is difference between inverter and buffer?

if you connect Nmos and Pmos other way around then it act as buffer


Difference between character and string?

A string ends with a '\0' character,but character is not.


What is the difference between TAE buffer and TE buffer?

The main difference is in composition. In TE common Tris buffer is bring down to pH 8 with HCl and EDTA is involved but in TAE instead of Tris HCl in TE Tris-acetate buffer is used.


What is the difference between Basel II and Basel III?

There is a main difference between Basel II and Basel III. In Basel III, there is a 4.5% capital buffer to absorb shock. With Basel II, there is no capital buffer.


What is the difference between rope and string?

The difference between thread rope and string is that thread is more thicker than string and that string is more thinner than rope and thread is more thinner than rope there's your answer geese


What is the difference between phosphate buffer and phosphate buffered saline?

actualy there is no major difference , on the basis of composition ,can be differentiate in saline buffer nacl was used and isotonicity take place


Whats The difference between a 6 and 7 string guitar?

There is practically no difference. The 7-string guitar has one lower string that the 6-string does not. It is usually tuned to B(natural).