Bits of Java – Episode 10: String Pool
Today we will discuss about the String Pool. What is it? Well, in the last post we talked about the fact that
String are immutable, and that every time you manipulate a
String you are actually creating a new object in memory, which is, for sure, not the most efficient approach.
In this sense, you can think about the String Pool as a space in memory where Java stores literal values and constants, that can be reused multiple times. This does not mean that
String stop to be immutable and all the things that we discussed before are no more valid!
It simply means that, when you create a
String, under certain conditions that we will see in a moment, Java first looks in the String Pool whether the same object exists, and, if so, it returns you a reference to it!
What are the certain conditions I was talking about? First of all, not with all the ways of creating a
String we are saying to Java to look in the pool! Let’s see an example:
String name = "Ilenia"; String sameName = "Ilenia"; String againSameName = new String("Ilenia");
When you create a
String using a literal, this tells Java to first look in the pool, and, if no object like this is found in the pool it will be added. This means that in the first line of our example, Java will look in the pool for such a
String object, it probably does not find it assuming this happens very early in our program, and so it adds it. In the second line we are creating again a
String using a literal, which is the same as before; thus, this time Java will find an object in the pool and it would set
sameName to reference that object.
The third line, on the other hand, is different. We are creating a
String with the same content of the other two, but this time we are not using a literal, but the
new keyword. This forces Java to really create a new object, even if in the pool it would have something appropriate for it!
The concept of the String Pool is important in terms of efficiency, but it can also create some confusion, especially when it comes to the equality operator (
You probably all know that when you apply the
== operator to a reference variable, what it does is checking whether the objects referenced by the two operands are actually the same object. Thus, this is a good point where, knowing what the String Pool is and what it does can make the difference!
String name = "Ilenia"; String sameName = "Ilenia"; String againSameName = new String("Ilenia"); System.out.println(name == sameName); //prints true System.out.println(name == againSameName); //prints false
The first print statement will print
true, because the two variables actually point to the same object in the String Pool, while the second print statement will print
false, because we have explicitly asked Java to create a new object for the variable
Let’s complicate a bit the things, shall we? In general, if two
String objects do not evaluate to the same
String at compile time, a new object is created!
String name = "Ilenia"; String sameName = " Ilenia".substring(1); System.out.println(name == sameName); //prints false
In this case, the second line is evaluated at runtime, and so the objects the two variables are referring to are actually two different objects, resulting in the print statement displaying
String name = "Ilenia"; String sameName = "Ile" + "nia"; String againSameName = "Ile"; againSameName += "nia"; System.out.println(name == sameName); //prints true System.out.println(name == againSameName); //prints false
Again, the second line is a concatenation which will be evaluated at compile time, and so the two variables,
sameName, actually reference to the same object; while the third variable is concatenated at runtime, resulting in a different object to be created!
String has also a method
intern(), to force Java to look in the pool and use an object from there if there is.
String name = "Ilenia"; String againSameName = new String("Ilenia").intern(); System.out.println(name == againSameName); //prints true
In this case we are creating the
String using the
new keyword, but then we are using the
intern() method, thus Java actually will use the pool and find a suitable object, which is the same as the one referenced by
name, making the two variables equals (in the sense of the
I hope you enjoyed the discussion and you learned something new too!
Next week we will abandon the
String and start talking about
Array, in particular about
by Ilenia Salvadori