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 do heavy weight components mean in java programming?

0 Answers  


I want to re-reach and use an object once it has been garbage collected. How it's possible?

0 Answers  


Difference between error and exception

3 Answers   Nous, TCS,


What are data types in programming?

0 Answers  


What are autoboxing and unboxing? When does it occur?

0 Answers  






What is the Concept of Encapsulation in OOPS

0 Answers   HCL,


What is independent and dependent variables in research?

0 Answers  


Explain the difference between jdk, jre, and jvm?

0 Answers  


Explain an algorithm to find depth of a binary tree.

0 Answers   Akamai Technologies,


Does java support function overloading, pointers, structures, unions or linked lists?

0 Answers  


What is busy spin, and why should you use it?

0 Answers  


how cani read a command line argument?(usingfile object).

3 Answers  


Categories