0

i have basic confusion .

String s2=new String("immutable");
System.out.println(s2.replace("able","ability"));
s2.replace("able", "abled");
System.out.println(s2);

In first print statement it is printing immutability but it is immutable right? why so? and in next printing statement it is not replaced> any answers welcome..

6
  • 1
    Immutable and RValue ! Commented Sep 19, 2013 at 6:43
  • This tutorial will help you understand string immutability concept well. thejavageek.com/2013/06/17/string-immutability-in-java Commented Sep 19, 2013 at 6:47
  • what is that i didnt get? @RongNK Commented Sep 19, 2013 at 6:48
  • store the second replace statement's result in a variable and the changes will be reflected. s2.replace does create a new string but you are not storing that any where and the orignal object remains the same. Commented Sep 19, 2013 at 6:51
  • @Raj if you set String s3 = s2.replace("able", "abled"); you will get the result. You have some below explanations. Commented Sep 19, 2013 at 6:52

6 Answers 6

6
System.out.println(s2.replace("able","ability"));

In above line, A new string returned and printed.

Because String#replce()

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

s2.replace("able", "abled");

It does the replace operation but,not assigned the result back.So the original String remains the same.

You see the result if you assign the result.

like

String replacedString = s2.replace("able", "abled");
System.out.println(replacedString );

or

s2= s2.replace("able", "abled");
System.out.println(s2);

Update:

When you write line

System.out.println(s2.replace("able","ability"));

That s2.replace("able","ability") resolved and returned String passed to that function.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure but are you sure that no new string is Generated?
-1! It does return a new String, just that its not assigned to anything.
even in the second line also i have not assigned to any variable
3

The replace(String,String) method returns a new String. The second call to replace() returns the replacement but you don't assign it to anything, then when you print out the immutable s2 again, you see the unchanged value.

1 Comment

I would add that String in Java is immutable and always creates a new String on changes.
2

String#replace returns the resulting String without modifying the original (immutable) String value...

You will get the same result if you assign the result to another String, for example

String s2=new String("immutable");
String s3 = s2.replace("able","ability");
System.out.println(s3);
s2.replace("able", "abled");
System.out.println(s2);

Will give you the same out put...

Comments

1

Lets look at line - 2:

System.out.println(s2.replace("able","ability"));

This will print immutability, this is because

s2.replace("able","ability")

will return another string, which is fed like:

System.out.println(tempStr);

But in third statement,

s2.replace("able", "abled");

There is no assignment to another variable, so a string is returned but not assigned to any variable. Hence lost, but s2 remain as is.

Comments

0

Immutable objects are simply objects whose state (the object's data) cannot change after construction

Your code s2.replace("able","ability"), it return a new String and nothing happen for s2.

And because replace function return a new String, so you can print the result by System.out.println(s2.replace("able","ability"));

String is Immutable, but String have a lot of methods that you can use as Rvalue

See also:

Comments

0

String s2=new String("immutable");

1)When ever we create a String as above a new object is created.If we are trying to modify it, a new object is created with the content we are providing and our String s2 is not modified.

2)If we need the modified value in the s2 object then replace the above code as..

String s2=new String("immutable");//Creates a new object with content 'immutable'
System.out.println(s2.replace("able","ability"));//creates a new object with new content as //immutability
s2=s2.replace("able", "abled");//Here also it creates a new object,Since we are assigning it //to s2,s2 will be pointing to newly created object.
System.out.println(s2);//prints the s2 String value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.