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


Please Help Members By Posting Answers For Below Questions

How do you stop a thread in java?

821


How does regex work?

735


What is oops in java?

767


What is integers and example?

704


How many bits is a float?

753


Mention a package that is used for linked list class in java.

740


Why should we use singleton pattern instead of static class?

715


What is meant by main method?

791


Difference between character constant and string constant in java ?

782


What is the flag in java?

798


What modifiers may be used with an inner class that is a member of an outer class in java programming?

772


When do I need to use reflection feature in java?

827


What is better - 'bit-shift a value' or 'multiply by 2'?

929


What is floor in java?

805


Explain java coding standards for classes or java coding conventions for classes?

878