String Pool : String values returned by String methods – are they stored in the String Pool?

(Series on String Pool : Part 2 of 4)


If you are preparing for your OCA Java SE 8 Programmer 1 certification exam, this post on String pool and String methods is for you.

String values returned by the methods of class – are they stored in the String Pool?

Answer to this question is not so simple. It depends on:

a) whether the String instance on which a method is called, comes from the String pool.
b) the values that are passed to the String methods.

The OCA Java SE 8 exam will include questions to test whether a String value returned by a String method, can be equal to an existing String instance. These values might be compared using the == operator or the equals() method.

Calling String methods on String instances that aren’t in the String pool

A String instance, which is created using a String constructor (by using the ‘new’ operator), isn’t placed in a String pool. When you can call String methods on such instances, the returned String values are never placed in the String pool.

You’ll find out the reason for this, in the section that follows.

Let’s cover a few String methods in this post, starting with the method trim(). To get a hang of these concepts, I’ll take you through the source code of these methods, from class String.

Method replace()

In the following source code, you can see that the replace() method either returns the String instance that it is called on (by returning ‘this’), or creates and returns a new String instance, by using the ‘new’ operator.

The method replace() returns the String instance, it is called on, if the oldChar and newChar that are passed to it are same, including their cases (uppercase/ lowercase).

For the OCA Java SE 8 exam, it would mean that the following code will output ‘true’:

String eJava = new String("Hawaii");
String guru = eJava.replace('a', 'a');
System.out.println(eJava == guru);

And the following code will also output ‘true’:

String eJava2 = "Hawaii";
String guru2 = eJava2.replace('a', 'a');
System.out.println(eJava2 == guru2);

What happens when the old and new char that you pass to the replace() method aren’t same. Here’s a quick example, which will output false:

String temp = "Hawaoo";
String eJava = "Hawaii";
String guru = eJava.replace('i', 'o');
System.out.println(temp == guru);

The following code will also output ‘false’.

String temp2 = new String("Hawaoo");
String eJava2 = new String("Hawaii");
String guru2 = eJava2.replace('i', 'o');
System.out.println(temp2 == guru2);

But if you compare the variables ‘temp2’ and ‘guru2’ using the equals() method, the code will output ‘true’. Here’s the modified code:

String temp2 = new String("Hawaoo");
String eJava2 = new String("Hawaii");
String guru2 = eJava2.replace('i', 'o');
System.out.println(temp2.equals(guru2));         // outputs true

What happens it you call the String method replace() that accepts two String method parameters?  Will they output the same result? Open the source code of class String and verify it yourself.

Method substring()

In the following source code, you can see that the substring(int) and substring(int, int) method either returns the String instance that it is called on (by returning ‘this’), or creates and returns a new String instance, by using the ‘new’ operator.

Method trim()

Again, method trim() returns String instances pretty much on the same logic as methods substring() and replace(). trim() returns the String instance on which it is called, if no white spaces need to be removed from it, otherwise, it creates and returns a new String instance.

Here’s the source code of method trim():

Method concat()

Here’s the source code of method concat():

You can expect the concat() method to return a new String instance, if the method parameter passed to concat() is not null.

Here’s a quick sample OCA Java SE 8 question for you, based on the topics covered in this article:

What is the output of the following code?

String old = "1.5";
String oldest = "1.0";
String current = old.replace('.', '.');
String latest = oldest.replace(".", ".");
boolean result = old == current && oldest == latest;
if (result)
    old.concat("Hello ").trim();
else
    old.concat("Hello").substring(0, 5);
System.out.println(old);


a) true
b) false
c) 1.5
d) 1.5He
e) 1.5Hello
f) Compilation error
g) Runtime exception

Post your answers in the comments.

Happy coding.

With much respect,
Mala

4 Replies to “String Pool : String values returned by String methods – are they stored in the String Pool?”

    1. Thanks for posting your answer. Seems like you couldn’t execute this code on your system. Give it a try! 🙂

      With much respect,
      Mala

  1. C is the answer

    the trick is
    equality > logical so
    result = (old == current )&& (oldest == latest);

    then replace the same value returns the original value without modification

    result = true;

    then concat does not change the value of the String

    1. Hi Iván –

      Your answer and reasoning is correct. Since String instances are immutable, calling concat() or tim() will not change its value. The code will output 1.5.

      With much respect,
      Mala

Leave a Reply

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