Confusion on string immutability
I have following code:- public class StaticImplementer { private static String str= "ABC"; public static void main(String args[]) { str = str + "XYZ"; } } Questions:- Here String str is static, then where this object will be stored in memory? Since String is immutable, where the object for "XYZ" will be stored? Where will be final object will be Stored? And how will garbage collection will be done? Here String str is static, then where this object will be stored in memory? String str is not an object, it's a reference to an object. "ABC" , "XYZ" & "ABCXYZ" are three distinct String objects.