Relative Content

Tag Archive for strings

What’s the difference of an object being final and an object being immutable in java?

final String str = “do not change me”; str = “why not?”; //it will result in compile time error saying that final fields can not be re-assigned once created i.e. the reference cannot be changed String str1 = “hello world”; System.out.println(str1.replaceAll(“h”, “q”)); str1 = str1.replaceAll(“h”, “q”); System.out.println(str1); //An immutable object on the other hand means […]

What’s the difference of an object being final and an object being immutable in java?

final String str = “do not change me”; str = “why not?”; //it will result in compile time error saying that final fields can not be re-assigned once created i.e. the reference cannot be changed String str1 = “hello world”; System.out.println(str1.replaceAll(“h”, “q”)); str1 = str1.replaceAll(“h”, “q”); System.out.println(str1); //An immutable object on the other hand means […]