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

What is a superclass?

0 Answers  


What is the importance of static variable?

0 Answers  


What is the gregoriancalendar class in java programming?

0 Answers  


What is a java lambda expression?

0 Answers  


What is the program development process?

0 Answers  






What are the Class Libraries ?

0 Answers   Wipro,


What is the difference between error and an exception?

0 Answers  


What is the difference between static and non-static variables?

6 Answers  


What is JFC?

0 Answers  


What is object-oriented paradigm?

0 Answers  


What is a nullable field?

0 Answers  


What is thread?

7 Answers   BMC, Virtusa,


Categories