0

How many objects are created by the following 2 lines of Java code?

String s = new String("Hello");
String s1 = s.toUpperCase();

I know toUpperCase() will create a new object in the heap. But I want to know if it will also be placed in the String pool.

rgettman
  • 176,041
  • 30
  • 275
  • 357
user3444096
  • 21
  • 1
  • 5
  • 1
    In the title, you ask “How many String objects are created…”, in the body text, you ask “how many objects will be created…”. This is not the same. Further, the last sentence creates an entirely different question. – Holger Sep 28 '20 at 09:53

3 Answers3

3

The answer is ... it depends.

The one thing that we can say with certainty is that neither toUpperCase() or subString() will place a String in the string pool1. The only String operation that is specified as (possibly) adding a String to the string pool is intern().

We cannot say for certain that toUpperCase() and subString() will generate a new String object. For example, in Java 11 str.substring(0) returns str itself. This is an implementation detail, but the javadocs for many operations are worded so that it would be valid to return this ... if this satisfied the requirements.

In the cases when a new String is created, it is implementation dependent how may objects are created. A Java string is typically represented as a String object with a reference to an backing char[] or byte[] that represent the string's characters. But in some Java releases, a backing array can be shared between multiple String objects ... under certain circumstances. And indeed substring was one of the methods that would create String with shared backing arrays.

Finally, as a commenter pointed out, this stuff is irrelevant to real world Java programming ... in all but the most extreme circumstances. Normally, you should just let the JVM deal with this kind of stuff.


1 - It is the string pool not the "constant string pool". The "constant pool" is actually something that exists in class files. But the string pool may contain strings that do not correspond to Java constants.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

No.

You can test it by running the following code.

    String s = new String("Hello");
    String upperS = "HELLO";
    String s1 = s.toUpperCase();
    System.out.println(s==s1);  //prints false
    System.out.println(upperS==s1);  //prints false

From §3.10.5 of the Java documentation:

…a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.29) - are "interned" so as to share unique instances, as if by execution of the method String.intern (§12.5).

So, the String Pool holds only string literals from the Java source code (expressions like "abc") and strings that have had their .intern method called.

In your code the following line creates a String object on the heap, but not a literal in the String Pool.

    String s = new String("Hello");

The following line also creates another object in the Heap and doesn't create a literal in the String Pool.

    String s1 = s.toUpperCase();

So, in your example. 2 objects will be created in the heap. No new literals will be added to the String Pool.

Take a look at this answer

Abhinav S.
  • 30
  • 7
0

Correct me if I'm wrong; the "String Constant pool" is just a section of the heap, specifically for String values.
So, what is created in the heap, is the pool you're referring to.

It's +2 String objects for String s = new String("Hello").
And, +1 String object for String s1 = s.toUpperCase().

Additionally, most IDEs allow you to view the memory during a debugging process.

Reilas
  • 3,297
  • 2
  • 4
  • 17