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 ?

Answers were Sorted based on User's Feedback



String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; ..

Answer / paras bist

above program is incorrect ,it will give compile time error
as "Duplicate Variable s1".
it should b
String s1 = "ABC";
String s = s1+"XYZ";
which is valid in string ,as we are creating new String
object

Is This Answer Correct ?    14 Yes 2 No

String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; ..

Answer / k.shiva

here we define s1="ABC".That means one new string is
created.And this string value is stored in heap its
refrence variable is s1.when we say s1+"XYZ".Then string s1
i.e ABC and XYZ are concadinated by using concadination
operater.It forms a new string i.e "ABCXYZ" in heap.But
again we assign this value to the refrence variable
s1.Then "ABCXYZ" value is assign to s1 and the string "ABC"
which is garbage collected.

Is This Answer Correct ?    12 Yes 4 No

String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; ..

Answer / sivadasan

It will Give Compile time Error...

We can not declare again s1.

So the Compiler will give

s1 is already defined in main(java.lang.String[])

Is This Answer Correct ?    6 Yes 0 No

String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; ..

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

String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; ..

Answer / vishal

in the later case a different overloaded contructor is
invoked while in the prior case a different.As in the later
case a different constructor initializes the string s1 with
different values hence the result

Is This Answer Correct ?    4 Yes 2 No

String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; ..

Answer / ramesh

Compilation ERROR
Duplicate Variable s1.

Is This Answer Correct ?    2 Yes 4 No

Post New Answer

More Core Java Interview Questions

Difference between process and thread?

0 Answers   HP,


what is difference between length and length()?

8 Answers  


Which class is the superclass of all classes?

0 Answers  


what is struts-config-xml?and its use?

3 Answers  


wat is class level lock and object level lock

3 Answers   HCL, Persistent,






What is class??

0 Answers   Tech Mahindra,


What means public static?

0 Answers  


Why to use nested classes in java? (Or) what is the purpose of nested class in java?

0 Answers  


What is difference between arraylist and list in java?

0 Answers  


What is the difference between class forname and new?

0 Answers  


What is thread life cycle?

0 Answers  


What is the base class in java from which all classes are derived?

0 Answers  


Categories