What is the cost of creating a Java String in relation to the JVM string group?


Java has a string pool and therefore before a new string is actually created I guess there is a check to see if the same string already exists, what is the cost of this call?

Is the pool a hash or tree based structure?

In either case I guess comes down to the performance of the String hash code, as search trees use hash code as a key?

I'm guessing its about log n, where n is the number of strings currently on the heap.

From this unofficial blog

Java Virtual Machine maintains an internal list of references for interned Strings ( pool of unique Strings) to avoid duplicate String objects in heap memory. Whenever the JVM loads String literal from class file and executes, it checks whether that String exists in the internal list or not. If it already exists in the list, then it does not create a new String and it uses reference to the existing String Object.

JVM does this type of checking internally for String literal but not for String object which it creates through 'new' keyword. You can explicitly force JVM to do this type of checking for String objects which are created through 'new' keyword using String.intern() method. This forces JVM to check the internal list and use the existing String object if it is already present.