Performance in Java string creation


What is the best way to create Java strings for better performance? Here are two examples of creating strings in Java:

Ex. 1:

String str = "string";

Ex. 2:

String str = new String("string");
asked by anonymous 03.06.2014 / 23:11

2 answers


@ QMechanic73's answer is good, though it raised a question I just researched to understand. Why does a==c and b==c always return false ?

String a = new String("foo");
String b = new String("foo");
String c = "foo";

System.out.println(a == c); // False
System.out.println(b == c); // False

To complement its response, when you create a String with the new operator you are creating an object for this String in heap and when creating the String without new you are looking for a String in pool de Strings , however if you do not have String in% in String is created, regardless of whether or not an String exists in the heap .

Therefore, only when comparing the two Strings created without the pool operator returns true .

A removed example from this site here.

When you do this:

String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // mesma referência
String s4 = new String("Hello");  // objeto String 
String s5 = new String("Hello");  // objeto String

If you have this:

Note that if new and s4 were created before s5 , s1 and s2 the diagram would not change, it would be the same as above.

Answering your question, the best way to create Strings is without the s3 operator, since the chances of reusing String are greater, since every time you String and it already exists, you do not need to create another one.

04.06.2014 / 00:29

In the first case it is always the same object being retrieved from the String Constant Pool in the second case, a new object is being created.

String str = new String("foo");

You force the creation of a new String object and it consumes a little time and memory.

String str = "foo";

str will only be created for the first time ( a new object ), and will be cached in #, so each time you refer to it in its literal form, you're getting exactly the same object, which is pretty fast.

String str = "foo";

The JVM performs some tricks while instantiating strings literals / objects to increase performance, and decrease memory overhead. To reduce the number of String objects created, JVM maintains a special memory called String constant pool . Each time you create a literal string, the JVM checks the String constant pool first.

If the string already exists in the String Pool , a reference to the instance is returned. If the string does not exist, a new String object is created and placed in the Pool. The JVM holds at most one object of any String in the Pool. Strings literals always refer to an object in the String Pool .

How does it work?

The JVM checks the String Constant Pool first, and if the string does not exist, a new String object foo is created and a reference is maintained in the Pool. The str variable also refers to the same object.

What if we have a statement like this?

String str2 = "foo";

The String Constant Pool and check the String Constant Pool and the String string already exists , a reference to the instance is returned to str2 . This statement does not create any String object in memory and str2 refers to the same object as str .

To verify this, you can compare two String references using the == (see also How to Compare Strings in Java? ) to verify that two references refer to the same String object in memory.

String str = new String("foo");
String str2 = new String("foo");
String str3 = "foo";
String str4 = "foo";

System.out.println(str == str2);   // false
System.out.println(str == str3);   // false
System.out.println(str3 == str4);  // true


str3 and str4 are the same object soon, comparison == is true.



  • new String () vs literal string performance
  • String Literal Pool
  • 03.06.2014 / 23:40