String is an immutable object.
Then how can the following code be justified.
String s1 = ?ABC?;
String s1 = s1+?XYZ?;
s.o.p(s1);
The output is ABCXYZ, which is the value of s1 ?
Answer Posted / amr
actually its a compiler error , variable already defined ..
but i'll answer the other part ,
String is immutable but does it make sense to write
String S1= abc;
S1=s1+"def";
YEAH its immutable and what happens behind the scene :
a new temporary String is created and using StringBuffer
which mutable class then appending then returning the
temporary string , thats the meaning of String is immutable
but StringBuffer is mutable , so if u r gonna use intensive
String manipulation u better go for StringBuffer because
String concatenation will result in more lines of code in
the byte code file generated when we compile .
this is a very frequent interview question .
| Is This Answer Correct ? | 4 Yes | 0 No |
Post New Answer View All Answers
What is difference between final and immutable?
Is it safe to install java on my computer?
Can I import same package/class twice?
what are the states associated in the thread? : Java thread
Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?
List out five keywords related to exception handling ?
What is variable length arguments in java?
What are inbuilt functions?
Explain the use of volatile field modifier?
What is navigable map in java?
Is object a data type?
Is c better than java?
Is a class subclass of itself?
Does string is thread-safe in java?
Can we define private and protected modifiers for the members in interfaces?