Why do constant strings use intern ()?

I know about the string pool in the JVM and the difference between literals and string objects. I know that literals are automatically interned, but what is the purpose of this line:

public static final String PARAMETER = "value".intern(); 

On my question, I always find a ton of text that explains the same thing to me, with an emphasis on the difference between literals and objects and the mention that literals are already interned. So I would like to learn about how to use this complex string with intern() over a literal.

+7
java string
source share
5 answers

The main advantage of this type of code is to prevent the compilation time constants from being constant.

For example, suppose you have a constant in one class, and you have many other classes that reference this constant. Usually, if you change a constant value, you will need to recompile all related classes. Using intern() in a field will prevent the embedding of a constant and means that it would be sufficient to recompile only your constant class. Theoretically, this approach would have low performance, although I do not know how significant this is.

This SO question covers the same topic and is useful for reading.

+12
source share

To prevent compilation time nesting

The only use case I can think of is to prevent it from being considered a compile-time constant so that it does not interfere with embedding.

Now, one of the reasons might be that someone could change the value with Reflection, which will only work if the line is declared like that.

It also allows you to modify a class file containing a constant, as @Duncan said in his answer. This is also a good reason.

How it works?

When you declare a String as public static final String CONST = "abc"; , then this is a compile-time constant and will be built into the class using this constant.

 class A{ private void foo(){ String newString = CONST + someDynamicValue; } } 

After compiling, if you decompile the class, you will find

 class A{ private void foo(){ String newString = "abc" + someDynamicString; } } 

But when you declare it using .intern() or any other method, it cannot be considered a compile-time constant and every time the value is retrieved from the class. Java makes it efficient.

Example from @Marko's comment:

the SWT library uses a similar trick to stop from incrementing an int constant, and the only reason should be consistent regarding updating the SWT library

+8
source share

Java automatically puts string literals. This means that in many cases the == operator works for strings in the same way as for int or other primitive values.

Since interning is automatic for string literals, the intern() method should be used for strings constructed with new String()

Using your example:

 String s1 = "Shoaib"; String s2 = "Shoaib"; String s3 = "Shoaib".intern(); String s4 = new String("Shoaib"); String s5 = new String("Shoaib").intern(); if ( s1 == s2 ){ System.out.println("s1 and s2 are same"); // 1. } if ( s1 == s3 ){ System.out.println("s1 and s3 are same" ); // 2. } if ( s1 == s4 ){ System.out.println("s1 and s4 are same" ); // 3. } if ( s1 == s5 ){ System.out.println("s1 and s5 are same" ); // 4. } 

will return:

 s1 and s2 are same s1 and s3 are same s1 and s5 are same 

The explanation in simple words:

-> In String pooled region , if we assume that we have "Shoaib" as a string, the link of which is 2020

-> Now, any object created using new String("Shoaib") will point to another link: 3030

-> But if you want to assign a reference from "Shoaib" in String of the merged region to new String("Shoaib") , then we use intern () on it.

So, above, you asked that the β€œvalue” .intern () does not make sense in the case of internment.

+1
source share

The intern () function is used to access the reference identifier of your constant string pool.

It is possible that your variable may contain the same value, so we do not need to select it again.

Are we just using intern () to check if it is already in the String Constant Pool or not?

If this is not a pool, then only the JVM allocates in the pool area and returns its reference identifier, and we used to share the same data item, and the advantage is that we use the same memory.

0
source share

Purpose of this line

 public static final String PARAMETER = "value".intern(); 

used only for checking string constants from the pool. If it is available, it returns again is not added to the pool.

0
source share

All Articles