What is the difference between
str.replaceAll(); //1
and
str = str.replaceAll(); //2
When I try the first one the compiler doesn't give error but the string is not changed at all but the second one works fine. What does the first one do? Sample code below:
String str = "abcdefgh";
str.replaceAll("abcd", ""); //1st replacement
System.out.println(str);
str = str.replaceAll("abcd", ""); //2nd replacement
System.out.println(str);
The output after 1st one is
abcdefgh
and after the second one is
efgh
I know we should use second one because it updates but still aren't String objects? We are like calling the replaceAll() method for it and it should change it but why it doesn't? Any ideas would be much appreciated.