Reputation: 55
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..
Upvotes: 0
Views: 108
Reputation: 166
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.
Upvotes: 0
Reputation: 4189
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:
Upvotes: 0
Reputation: 121998
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.
Upvotes: 6
Reputation: 4113
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.
Upvotes: 1
Reputation: 143856
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.
Upvotes: 3
Reputation: 347184
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...
Upvotes: 2