All you have demonstrated so far is that some strings which are identical have the same hash code. This is simply a rule of how hash codes behave and it has nothing to do with the system's string pools.
String pool is a storage space in the Java heap memory where string literals are stored. It is also known as String Constant Pool or String Intern Pool. It is privately maintained by the Java String class. By default, the String pool is empty. A pool of strings decreases the number of String objects created in the JVM, thereby reducing memory load and improving performance.
Carey Brown wrote:Output of histogram:
Note that the hash code is determined by the characters and sequence that they appear in the String. It has nothing to do with any "pool".
Whenever a string literal is created, the compiler checks the String Constant Pool first. If it encounters the same string, then instead of creating a new string, it returns the same instance of the existing string to the variable.
Norm Radder wrote:
yes !!!
Ok, which of the two choices is the one?
Or do you want both?
Norm Radder wrote:
determine the number of strings that are definitely created in the string pool
Given the hashCodes of 1,2,1,2,2 what is the desired results?
2 unique hashCodes
max count of 3 with same hashCodes