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 meant by design patterns?

575


How many types of design patterns are there?

524


what is the difference between Object Based Language and Object Oriented Language?

591


Explain different ways of creating a thread?

525


How do I start learning java?

553






What is the meaning of course?

575


What is the difference between conversation & casting?

566


What are methods and how are they defined?

601


What is return in java?

548


What flag up means?

593


What invokes a thread's run() method in java programming?

561


What are the new features in java 8? Explain

553


What is string example?

593


How to Sort Strings which are given in List and display in ascending order without using java api.

3752


What are variable names?

522