Difference between String & StringBuffer

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the loop in java?

576


What does s mean in regex?

553


Which keyword specify that a variable is effectively final ?

577


What is backdrop?

596


Can we assign the reference to this variable?

548






how would you implement a thread pool? : Java thread

510


What is r in java?

595


what is a thread pool in java and why is it used?

532


What are different types of constants?

520


What does jre stand for?

609


What’s the difference between unit, integration and functional testing?

616


What is java and its types?

553


What are wrapped classes in java programming?

589


What is null in java?

523


How to print an arraylist in java?

500