Strings are immutable. But String s=\"Hello\"; String s1=s+\"World\" returns HelloWorld how ?

Strings are immutable. But String s=\"Hello\"; String s1=s+\"World\" returns HelloWorld how ?

  String s=\"Hello\"; 
  String s1=s+\"World\";
In the second line above, We are trying to concatenating the \"Hello\" and a new strign object \"World\". Here a new object is created \"HelloWorld\" and returned to the reference s1. Since string objects are immutable, the reference s is still pointing to \"Hello\".

By:Jalees
Date:2015-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher