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
Describe different states of a thread.
What is user defined exception in Java?
What is the gregoriancalendar class in java programming?
What is an argument java?
Why 1 is not a prime number?
What is a final class in java?
What is general methodology?
What is %02d?
What is the purpose of the runtime class in java programming?
How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?
What's the base class of all exception classes?
Explain different types of wrapper classes in java?
What is the default size of arraylist in java?
What are the types of collections in java?
What will happen to the exception object after exception handling?