As we know Strings are immutable. But what happens to the reference of a string object if we replace them.
Please check the below example
public class Test {
public static void main(String[] args){
String s = "My name is Java"; -------> Step 1
s = s.replace("Java", "Python"); ---> Step 2
System.out.println(s);
}
}
In step one, Reference s pointing to object "My name is Java", but after replace its s is pointing to "My name is Python".
After replace who is pointing to "My name is Java" ?