-1

Please consider below 2 lines of Java code :

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

In the above code how many objects will be created when we call toUpperCase() method on String s. Will an object be created in heap as well as String constant pool or will it be created only in heap? I know toUpperCase() will create a new object in heap. But want to know if it will also be placed in the String constant pool.

user3444096
  • 11
  • 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

1 Answers1

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
  • 669,072
  • 92
  • 771
  • 1,162