Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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 the life cycle of Thread ?

12 Answers   HCL, Varnar Softech,


what is webservices?

1 Answers   CTS, Ericsson, HCL,


Which is better stringbuilder or stringbuffer?

0 Answers  


Is string pool garbage collected?

0 Answers  


How does enum work in java?

0 Answers  


What is a singleton factory?

0 Answers  


Can you explain the private protected field modifier?

0 Answers  


Any one can explain how the inerface uses in java. give with example.

1 Answers   IBM,


There are two classes named classa and classb. Both classes are in the same package. Can a private member of classa can be accessed by an object of classb?

0 Answers  


Define nashorn in java8.

0 Answers  


What are the two major components of JDBC?

5 Answers   CMC, Mind Tree,


Is an empty arraylist null?

0 Answers  


Categories