Interview question: how many strings have been created?
String S1 = new string ("hello"); string S2 = new string ("hello");/*** S1 creates two objects. The new string is in the heap, then Reference creating the "hello" area in the constant pool. * S2 creates only one object, that is, the new string () of the heap. Because the constant pool already has Hello, it directly references */system. out. println (S1 = S2); // false; system. out. println (s1.equals (S2); // true system. out. println ("*************************"); string S3 = new string ("hello"); string S4 = "hello"; system. out. println (S3 = S4); // falses3 references the heap, and directly references the hello that already exists in the constant pool in the reference "hello", S4 created in the constant pool, however, their address values are different. System. out. println (s3.equals (S4); // truesystem. out. println ("************************"); string S5 = "Chuanzhi "; string S6 = "Chuanzhi"; system. out. println (S5 = s6); // true // when S6 is assigned a value, it is directly referenced if "intelligence" is found in the constant pool. So S5 And S6 point to the same address value. System. out. println (s5.equals (s6); // trues5.substring (0, 1); // The truncated character. Instead of modifying the S5 value, it also opens up a storage space in the constant pool. Currently, it is not referenced by a variable. System. out. println (s5.equals (s6); // true system. out. println ("*************************");
String constant pool