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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do generics work in java?

524


String class is defined under which package in java?

625


What is Gang of four design patterns

595


What 5 doubled?

510


Can we instantiate interface in java?

674






What are the two ways in which thread can be created?

598


How do you create immutable object in java?

539


What about features of local inner class?

621


Garbage collection in java?

595


When should I use singleton pattern?

545


Can we force garbage collector to run ?

572


Why do we need strings in java?

526


What is a flag variable?

559


What is a local class in java?

587


Explain the difference between an object-oriented programming language and object-based programming language?

549