Does the final string inside the private static method execute an instance of the new object when called?

Does the static final string inside the private static method instantiate a new object when called?

private static String Test() { final String foo = "string literal"; return foo; } 

Or does the compiler know that there is only one string literal inside the method? Or should I make this a private static field of the final class? This results in less readability by spreading code around the class.

+4
source share
4 answers

No, a particular string will be reused from the string literal of the pool . If it was, for example:

 final String foo = new String("string literal"); 

Then a really new one will be created every time the method is called.

Here is the proof:

 public static void main(String[] args) throws Exception { String s1 = test1(); String s2 = test1(); System.out.println(s1 == s2); // true String s3 = test2(); String s4 = test2(); System.out.println(s3 == s4); // false } private static String test1() { final String foo = "string literal"; return foo; } private static String test2() { final String foo = new String("string literal"); return foo; } 

Note that the final modifier has no effect in this particular case. It prohibits reassigning a variable.

+8
source

For a string literal, only one instance of String . They are stored in the "user pool" of the String class. So, given these String initializations:

 String copy = new String("x"); String alias = copy.intern(); 

The operators copy != "x" and alias == "x" are true.

In this case, it would be much more readable if you just had a field.

+1
source

You do not need to worry too much about string literals. String literals receive special JVM processing to improve performance and reduce memory overhead. The string literal used anywhere in your code (local or other) is concatenated and reused by the JVM. This is done to reduce the number of String objects created in the JVM. Each time your code creates a string literal, the JVM first checks the string literal in the pool. If the row already exists in the pool, a link to the merged instance is returned. If the string does not exist in the pool, a new String object is created, then placed in the pool. Java can do this optimization because strings are immutable and can be shared without fear of data corruption. However, this behavior is true only for String literals, and not for objects created using the "new" keyword.

Thus, creating a string literal has the same effect as a class variable (a private static final field of a class) or saving it as a local variable.

+1
source

foo will be created every time the function is called, because it is a local variable inside the method. The fact that this particular method is static does not matter. foo, which is final, also does not affect when it was created, it just means that foo cannot change after the initial assignment.

Make this a private variable of the static class to ensure that it is created only once.

0
source

All Articles