Why do equal java strings accept the same address?

Possible duplicate:
Creating a String Object Using the New and Comparing It with the intern Method

I played with the Strings to understand them more, and I noticed something that I can not explain:

String str1 = "whatever"; String str2 = str1; String str3 = "whatever"; System.out.println(str1==str2); //prints true...that normal, they point to the same object System.out.println(str1==str3); //gives true..how that possible ? 

How does the last line give the truth? this means that both str1 and str3 have the same memory address.

Is this a compiler optimization that was smart enough to detect that both string literals are the same ("independently") and thus assign str1 and str3 to the same object? Or am I missing something in the basic row mechanics?

+8
java string javac
source share
4 answers

http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3

As the message says:

The distribution of strings, as well as the distribution of all objects, is expensive both in time and in memory. The JVM does some tricks when creating string literals to improve performance and reduce memory overhead. To reduce the number of String objects created in the JVM, the String class contains a string pool. Each time your code creates a string literal, the JVM first checks the string 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.

+5
source share

Because Java has a pool of unique interned instances and that string literals are stored in this pool. This means that the first “any” string literal is exactly the same string object as the third “any” literal.

As the document says:

public String intern()

Returns the canonical representation for a string object. A pool of rows that are initially empty is saved. privately by the String class.

When calling the intern method, if the pool already contains a string equal to this String object, defined by the equal (Object) method, then a string from the pool is returned. Otherwise, this String object is added to the pool and a link to this string is returned.

It follows that for any two lines s and t s.intern () == t.intern () is true if and only if s.equals (t) is true.

All literals and string constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Returns: a string that has the same contents as this string, but guaranteed from a pool of unique strings.


+7
source share

If you do:

 String str1 = new String("BlaBla"); //In the heap! String str2 = new String("BlaBla"); //In the heap! 

then you explicitly create the String object via the new operator (and the constructor). In this case, each object will point to a different repository .

But if you do this:

 String str1 = "BlaBla"; String str2 = "BlaBla"; 

then you have a hidden design. Literals of two lines use the same storage if they have the same values, because Java saves the memory of the same lines! (Lines having the same value)

+2
source share

The javac compiler combines string literals that are the same in a given class file.

However, at run time, string literals are concatenated using the same approach as String.intern (). This means that even strings in different classes in different applications (in the same JVM that uses the same object.

+1
source share

All Articles