-1

Can anyone answer me please?

public class ReplaceString{

    public static void main(String arg[]) {
        String a = "Hariom";
        a = a.replace('H', 'b');
        System.out.println(a);
    }
}
trincot
  • 317,000
  • 35
  • 244
  • 286
Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • It _is_ immutable; when you .replace() you create a new string object. Don't be mixing with immutable objects and object references – fge Apr 24 '15 at 07:27
  • You made `a` refer to a different String. The old String still contains Hariom, but now you have a new one as well, which contains bariom, and you made a point to bariom. – user253751 Apr 24 '15 at 07:29

6 Answers6

8

A String is immutable, which means you cannot change the string value on the same address. If you change it, it will create a new object and you have to assign it to the current string object. For example,

 String a = "Hariom";
 a.replace('H', 'b');
 System.out.println(a);   //will print "Hariom"

because a is not changed. Instead, you created a new String in memory with the value bariom and to show that change to a, you have to point the newly created string to a, i.e.

a= a.replace('H', 'b');

Maybe this would be a bit more clear:

Step 1: String a = "Hariom"; you create a new string in memory and the variable a points to it.


enter image description here


Step 2: a.replace('H', 'b'); if you replace some character in String a, a new String is created in the heap with no variable pointing to it.


enter image description here


Step 3: a= a.replace('H', 'b'); if you call replace() and assign it to the variable a, what happens is that the new String bariom is created in the heap. Then you assign it to a causing a to point to the new String.


enter image description here


ragingasiancoder
  • 616
  • 6
  • 17
singhakash
  • 7,891
  • 6
  • 31
  • 65
6

change your code to :

public static void main(String arg[]) {
        String a = "Hariom";
        String b = a.replace('H', 'b'); // returns a new string instance
        System.out.println(a==b); // prints false
    }

b and a are different objects. immutable means you cannot modify that object.

Note : Reflection can be used to break immutability of Strings.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
4
a = a.replace('H', 'b');

This doesn't modify the String a, it returns a new String instance (unless there is nothing to replace, in which case the original String is returned).

ragingasiancoder
  • 616
  • 6
  • 17
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Ok what about old String Hariom? how to get it ? And when this will garbage collected? – Subodh Joshi Apr 24 '15 at 07:46
  • @SubodhJoshi In this code snippet, there's no reference left to the original String, which means it becomes eligible for garbage collection. – Eran Apr 24 '15 at 07:49
2

According to Javadocs:

String#replace(char oldChar, char newChar) method Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar

String a = "Hariom";
a = a.replace('H', 'b');
/* method returns new instance of String ("bariom")
   and you referencing the new instance to variable a, 
   so now String object "Hariom" is no longer referenced by any variable
   and is available for garbage collection.
*/
System.out.println(a);
earthmover
  • 4,395
  • 10
  • 43
  • 74
0

The row

a.replace('H', 'b');

generate a new String. Then you set the a variable to the new value.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

a.replace does not modify a, it returns a new String object. If you call a.replace without reassigning to a, you will notice that the original String has not changed.

Variables can contain immutable objects, but you are still able to reassign to them. Reassigning does not change the object, it only changes which object the variable refers to.

Jacob Raihle
  • 3,720
  • 18
  • 34