Asked  7 Months ago    Answers:  5   Viewed   30 times

According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned.

So i tried this:

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".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.
}

I was expecting that s1 and s3 are same will be printed as s3 is interned, and s1 and s2 are same will not be printed. But the result is: both lines are printed. So that means, by default String constants are interned. But if it is so, then why do we need the intern method? In other words when should we use this method?

 Answers

46

Java automatically interns String literals. This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values.

Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String()

Using your example:

String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
String s4 = new String("Rakesh");
String s5 = new String("Rakesh").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

In all the cases besides of s4 variable, a value for which was explicitly created using new operator and where intern method was not used on it's result, it is a single immutable instance that's being returned JVM's string constant pool.

Refer to JavaTechniques "String Equality and Interning" for more information.

Tuesday, June 1, 2021
 
VostanAzatyan
answered 7 Months ago
46

Every compile-time constant expression that is of type String will be put into the String pool.

Essentially that means: if the compiler can (easily) "calculate" the value of the String without running the program, then it will be put into the pool (the rules are slightly more complicated than that and have a few corner cases, see the link above for all the details).

That's true for all the Strings in lines 1-3.

"Hel"+lo is not a compile-time constant expression, because lo is a non-constant variable.

The hash codes are the same, because the hashCode of a String depends only on its content. That's required by the contract of equals() and hashCode().

Friday, June 25, 2021
 
iceduck
answered 6 Months ago
73

seven is the first time you use the string 'hello2'. Therefor what the intern does is insert your string to the pool (and also return it). there for it is equal to your seven.

when you work with eight, the string is already in the pool (by running seven.intern() before, therefor when you do eight == eight.intern() you will get on the left side of the equation the newly created eight string, and on the right side the string created by seven from the pool, which are not the same

Friday, July 30, 2021
 
Jeremy Pare
answered 4 Months ago
32

A simple grep of the SDK shows us that the answer is that it doesn't matter—they are the same. They both turn into __T(x).

C:...Visual Studio 8VC>findstr /spin /c:"#define _T(" *.h 
crtsrctchar.h:2439:#define _T(x)       __T(x) 
includetchar.h:2390:#define _T(x)       __T(x)

C:...Visual Studio 8VC>findstr /spin /c:"#define _TEXT(" *.h 
crtsrctchar.h:2440:#define _TEXT(x)    __T(x) 
includetchar.h:2391:#define _TEXT(x)    __T(x)

And for completeness:

C:...Visual Studio 8VC>findstr /spin /c:"#define __T(" *.h 
crtsrctchar.h:210:#define __T(x)     L ## x 
crtsrctchar.h:889:#define __T(x)      x 
includetchar.h:210:#define __T(x)     L ## x 
includetchar.h:858:#define __T(x)      x

However, technically, for C++ you should be using TEXT() instead of _TEXT(), but it (eventually) expands to the same thing too.

Monday, August 2, 2021
 
ranhan
answered 4 Months ago
93

Start without extracting constants. Then later on, your editor can probably do this for you if you need it for some reason later on.

Though if you think it will improve the readability for your code, then you can use constants. Do this if you can add more semantic meaning by doing so:

BROWSER_WIDTH = "width"
CONNECTION_POOL_MAX_SIZE = "max_size"
Sunday, October 31, 2021
 
Extrakun
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share