This question has already been answered in the link below:
I'm confused by String Pool in Java. I got to know String Pool while I was reading the String chapter of Java. Please let me understand in easy words so that I can understand. What does String Pool actually do?
java
The code below prints true (even if you didn't use the equals method, it's the correct way to compare strings).
String s = "a" + "bc";
String t = "ab" + "c";
System.out.println(s == t);
When the compiler optimizes string strings, it is determined that both s and t have the same values and only one string object. In Java, String is safe because the value does not change. As a result, s and t point to the same object and save memory.
The name 'string pool' comes from the concept that all predefined strings are stored in 'pool' and the compiler checks for the same string value before creating a new String object.
© 2024 OneMinuteCode. All rights reserved.