Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "HelloWorld".How ?

Showing Answers 1 - 18 of 18 Answers

anasthesia

  • Nov 14th, 2005
 

String is immutable means that : Say s = "hello" would point to certain location , say location 1234h in memory.Now if we modify s = "world" , then s will point to a new location in memory, i.e 2314h(say).Thats the reason String is considered to be immutable, one cannot change contents in same meory location.Acc to this question, String s1 is just appended with s.So it doesnt necessarily show that s1 is mutated

  Was this answer useful?  Yes

Pritesh Vikram

  • Nov 14th, 2005
 

I came across this question sometime back and have found it very interesting that such questions check the basic concepts of Java .I am looking forward to more questions of this type and there reply too. Thanks and Regards, Pritesh Vikram S/w Devloper, Alchemy Infotech Pvt. Ltd. (Country Partner OF BroadVision Global Services.)

  Was this answer useful?  Yes

monika

  • Nov 14th, 2005
 

Strings are immutable.But String s="Hello";String s1=s+"World";S.O.P(s1); means printing "HelloWorld".How ?

  Was this answer useful?  Yes

SailajaT

  • Nov 15th, 2005
 

Strings are constants; their values cannot be changed after they are created. String objects can be shared. Whenever we are declaring String s= "Hello" then a new String object is created in memory. If we are doing any operation on String s then that results a new String. For ex. s.concat("abc") returns a new String object but the value of String s remains the same. Similarly when we are writing String s1=s+"World" in this case you are creating s1 as a new object which has the value of String s appended by String "World". By doing so we are not changing the value of s.

  Was this answer useful?  Yes

mohana

  • Nov 15th, 2005
 

Immutable strings are immplemented efficiently than changeable one,because there is companion class to string called STRINGBUFFER ,whose object contains the string that can be modified after they are created these class are avialable to all class automatically,string and strinbuffer implement the "charsequence" interfacestring instance can't be changed after once declared.

  Was this answer useful?  Yes

mandyjoshi

  • Nov 15th, 2005
 

immutable means un-changeable

So String s = "Hello";

means s is refering to some memory location where you have value ("Hello")

now you do s = s + " World";

so now s will refer to new memory location where new value of s stored ("Hello World") and old memory location is not free yet (wasted) until garbage collected.

  Was this answer useful?  Yes

Barsana

  • Nov 16th, 2005
 

Strings are immutable in the sense, "once created ,a string can not be changed".which means the String "Hello" can not be changed.But this does not mean that the reference variable which points to "Hello" can not be changed. so we can change the reference variable to point to anywhere else.

  Was this answer useful?  Yes

Srinivas

  • Jan 19th, 2006
 

Strings are immutable, means when ever a new value is assigned to the string variable, in the back ground you are really creating another string object and telling JVM to use the newly created string object as the place holder for a variable.

In the above example you mentioed, you are not assigning any new value to the already assigned value. It's just a concatination.

  Was this answer useful?  Yes

puviyarasan

  • Mar 24th, 2006
 

Strings are immutable.But
String s="Hello";
String s1=s+"World";
S.O.P(s1); means printing "HelloWorld".How ?

Strings are immutable is correct, this example are not change an value of S , create a an new variable S1 value is hello world.immutable means we cant change an value of S. 

Boopesh Potluri

  • Apr 27th, 2006
 

Hi,

Strings r immutable in nature. we can't change it's state in it's entire life time once they are created.

The answer for your question is both the strings will has diff hashcodes in jvm please check the hashcodes once.

  Was this answer useful?  Yes

rams_k

  • May 10th, 2006
 

strings are immutable here it is not over ridding instead its creating another instance and mapping that s to helloworld and the hello still exists in memory  and if u create another oj h as hello now the h will b pointing to that  hello

its hoe java works we can say that strings are immutable objects

  Was this answer useful?  Yes

siva kumar reddy

  • Aug 10th, 2006
 

hi.

very intresting and tricky question.

string objects are surely immutable.because of jvm takes more time to reallocate the memory than create a new object.thats why jvm create new object and stores not modify the old object.

cheers

siva

  Was this answer useful?  Yes

talktoatish

  • Aug 15th, 2009
 

Very tricky question.

Asked me in Oracle interview for 4 years level of exp

Here is the answer

: When JVM see the code where two three string objects has been added with + operator then it is using the String optimzation technique which varies from JDK version to version

In JDK 1.4 JVM converts the string objects to Strinbuffer and then adds all together and finally toString() method is called. So only one new object will be created irrespective of how many strings has been added.

for JDK 1.5 StringBuilder is used instead of StringBuffer as StringBuffer is slow compared to StringBuilder

  Was this answer useful?  Yes

String s = "Hello";
 means one object with value "Hello" will be created in the heap and reference variable s will refer to the object.

but when
String s1 = s+"World";
will be executed, there will be one new object with value "Hello World" created in the heap and reference variable s1 will refer to the object "Hello World".

What it indicates that object "Hello" is different object and "Hello World" is different object in the heap and we can't manipulate the value of these objects that's why String objects are called as immutable.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions