In Java, the string pool is a special storage area in the heap for strings. When you create a string literal, Java checks to see if that string already exists in the string pool. If it does, Java returns a reference to the existing string; if not, it adds the new string to the pool. Interning strings is a method used to ensure that all identical string literals share a single memory reference.
public class StringPoolExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello").intern();
System.out.println("str1 == str2: " + (str1 == str2)); // true, both refer to the same string pool instance
System.out.println("str1 == str3: " + (str1 == str3)); // true, str3 is interned and refers to the same instance
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?