Difference between String & StringBuffer

Answers were Sorted based on User's Feedback



Difference between String & StringBuffer..

Answer / sathish

Here the compiler does a good job of optimization. Compiler
simply concatenates at compile time as
shown below. It does compile time resolution instead of
runtime resolution, this happens when you
create a String object using 'new' key word.
before compilation:
String result = "This is"+"testing
the"+"difference"+"between"+"String"+"and"+"StringBuffer";
after compilation
String result = "This is testing the difference between
String and StringBuffer";
String object is resolved at compile time where as
StringBuffer object is resolved at run time. Run time
resolution takes place when the value of the string is not
known in advance where as compile time
resolution happens when the value of the string is known in
advance. Here is an example.
Before compilation:
public String getString(String str1,String str2) {
return str1+str2;
}
After compilation:
return new StringBuffer().append(str1).append(str2).toString();
This resolves at run time and take much more time to execute.

Is This Answer Correct ?    3 Yes 0 No

Difference between String & StringBuffer..

Answer / akshay

Akshay - "Difference String and String Buffer"

Is This Answer Correct ?    1 Yes 0 No

Difference between String & StringBuffer..

Answer / srikanth reddy

-->String objects are constants and immutable where as
StringBuffer objects are mutable
-----------------------------------------
-->in string class we can create object by two ways
1)using new operator --> string s = new String("Java");

2)without new operator --> String s2="raju";

------------------------------------------
-->in string class there string constant pooling technique
but in stringbuffer there is no facility
-------------------------------------------
all the methods in stringbuufer class is synchorined so
thread safe but not in string class

Is This Answer Correct ?    0 Yes 0 No

Difference between String & StringBuffer..

Answer / charan

A String is immutable, i.e. when it's created, it can never change.
A StringBuffer (or its non-synchronized cousin StringBuilder) is used when you need to construct a string piece by piece without the performance overhead of constructing lots of littleStrings along the way.

The maximum length for both is Integer.MAX_VALUE, because they are stored internally as arrays, and Java arrays only have an int for their length pseudo-field.
The performance improvement between Strings and StringBuffers for multiple concatenations is quite significant.

If you run the following test code, you will see the difference. On my ancient laptop with Java 6, I get these results:
Concat with String took: 1781ms
Concat with StringBuffer took: 0ms

Code:

public class Concat
{
public static String concatWithString()
{
String t = "Cat";
for (int i=0; i<10000; i++)
{
t = t + "Dog";
}
return t;
}
public static String concatWithStringBuffer()
{
StringBuffer sb = new StringBuffer("Cat");
for (int i=0; i<10000; i++)
{
sb.append("Dog");
}
return sb.toString();
}
public static void main(String[] args)
{
long start = System.currentTimeMillis();
concatWithString();
System.out.println("Concat with String took: " + (System.currentTimeMillis() - start) + "ms");
start = System.currentTimeMillis();
concatWithStringBuffer();
System.out.println("Concat with StringBuffer took: " + (System.currentTimeMillis() - start) + "ms");
}
}

Is This Answer Correct ?    0 Yes 0 No

Difference between String & StringBuffer..

Answer / tashu

1)String class is of fixed length, but stringbuffer class is
of variable length.

2)String class can't insert character or sub string in the
middle of string, but in Stringbuffer class this is possible.

Is This Answer Correct ?    1 Yes 1 No

Difference between String & StringBuffer..

Answer / ramesh raju

String s1="prabas";
String s2="raju";
String s3=s1.concat(s1);//s1=s2.concat("xyz") its not work
We didn't chang the content of s1,s2 strings
that's y String is Immutable

where as StringBuffer is mutable we can easly modified the
content

Is This Answer Correct ?    26 Yes 43 No

Post New Answer

More Core Java Interview Questions

What is the difference between error and an exception?

0 Answers  


what invokes a threads run() method? : Java thread

0 Answers  


What are the legal parameters?

0 Answers  


How do you classify Dialog Box?

0 Answers   CGI,


Tell us something about set interface.

0 Answers  






Is null or empty java?

0 Answers  


Can i have abstract class with no abstract methods?

22 Answers   CTS,


What is a singleton in genetics?

0 Answers  


Explain the difference between an object-oriented programming language and object-based programming language?

0 Answers  


Discuss 2D arrays.

0 Answers   Amdocs,


What is the use of optional ?

0 Answers  


How many digits can a float hold?

0 Answers  


Categories