In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable.[1] Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.
public class Main {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
String hi1 = "hi";
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String hi2 = new String("hi");
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}
}
public class Main {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
String hi1 = "hi";
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String hi2 = "hi";
}
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
}
}