String Pool : Why are String instances discriminated based on how they are created?

(Series on String Pool : Part 1 of 4)


Imagine how you would feel if you and your siblings were treated differently, depending on how you all were born.

The ones who were born in hospital A, can reside in a lavish resort with a pool. The ones who were born in hospital B, can’t reside in ‘the’ lavish resort with the pool.

Instances of class String are treated differently, by the JVM, depending on how they are created.

Welcome to the String Pool

The String Pool includes String instances, which are created using String literal values. The ones created by using the String constructor aren’t placed in the String pool.

Why is a String pool required?

String instances are widely used in Java. They are immutable, that is, once created, their value can’t change. By caching strings, JVM can save memory and improve performance. It ‘might’ refer to the existing strings, rather than creating new ones.

Why is this topic important in the OCA Java certification?

To ensure that new Java developers understand the difference between equality of String reference variables and equality of the values referred by them.

What’s the difference between equality of reference variables and equality of their values?

Equality of String variables (or any other type), is determined using the comparison operator, that is, ==.

A quick example:

String eJava = new String("OCA");
String guru = new String("OCA");
System.out.println(eJava == guru);


The preceding code will output false because the variables ‘eJava’ and ‘guru’ refer to different String instances.
Here’s another example, which checks the equality of values referred by the variables ‘eJava’ and ‘guru’, by using the method equals():

String eJava = new String("OCA");
String guru = new String("OCA");
System.out.println(eJava.equals(guru));

Since the variables ‘eJava’ and ‘guru’ refer to instances that store the same String value (‘OCA’), the preceding code will return true.

This seems okay. What is the problem?

Often new Java programmers mistake the comparison operator, that is, ==, for checking the equality of the ‘values’ referred by Java objects.

Due to the way the String instances are stored in a pool (or cache), their otherwise buggy code works okay. A quick example:

String eJava = "OCA";
String guru = "OCA";

System.out.println("Strings are equal : " + (eJava == guru));

In the preceding code, a new Java programmer is trying to check for the equality of the values referred to by the variables ‘eJava’ and ‘guru’. Though the code outputs true, a similar logic for the same programmer might give ‘incorrect’ output. Here’s another example:

String eJava = new String("OCA");
String guru = new String("OCA");

System.out.println("Strings are equal : " + (eJava == guru));

The preceding code will output false.

 


Tomorrow, I’ll share more interesting facts about the String pool.

Till then, Happy Coding!

With much respect,
Mala Gupta

Author of Manning’s OCA Java SE 8 Programmer I Cert Book.

Leave a Reply

Your email address will not be published. Required fields are marked *