Java的StringPool是存放在方法区中,是所有类共享的吗?
巴扎黑
巴扎黑 2017-04-17 11:16:07
0
4
932

Java虚拟机中,每个类都有一个常量池,StringPool存在常量池中,对于A.StringA.intern()==B.StringB.intern()应该返回False~~可是我打出来的是True,Why?

巴扎黑
巴扎黑

reply all(4)
阿神

Constant pool in each class:

Each class in Java has a constant pool. This constant pool definition is described in the .class file, including integers, floating point types, strings, class names, attribute names, method declarations, etc. When jvm loads the .class file, it will load these constant pool information, and according to the definition of JMM (java storage model), a memory space will be opened in the method area (often called PermGen Space [permanent generation]) for storage these constants.

It should be noted that:

Java will also maintain a string pool in the method area. When loading string constants from the constant pool, it will first store these string constants in the string pool, and then return its references to the constant pool of each class.

As for the String.intern() method, quote its JavaDoc:

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the {@link #equals(Object)} method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

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

The lines marked in bold also indicate that Java will maintain a string pool in the method area. As for why

A.StringA.intern()==B.StringB.intern() will return true

I believe you will understand after reading the javaDoc.

PS: Not only does the String class have a constant pool natively supported by the JVM, but the six primitive type wrapper classes of Boolean, Byte, Character, Short, Integer, and Long also implement code-level constant pools. , please refer to the JDK source code for details.
PPS: constant pool for reference

阿神

Java Doc:

"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. "

That is to say, when calling str.intern(), first go to the pool to find whether there is a string with equals(str)==true. After finding it, return the string that already exists in the pool, otherwise add str to the pool. in.

So in the pool, there will only be one copy of equal strings.

刘奇

Maybe there is something wrong with my question~, "The runtime constant pool is part of the method. In addition to the description information such as the class version, fields, methods, interfaces, etc., there is also another piece of information in the Class file which is the constant pool, which is used for Stores various literals and symbol references generated by the compiler. This content is stored in the runtime constant pool in the method area after the class is loaded. "I know the specific answer~ The constant pool in the class file information is only for running. A reference to the constant pool

伊谢尔伦

If the strings StringA and StringB have the same content, then the above statement StringA.intern()==StringB.intern() is true because they point to the same object.

After calling intern, first check whether there is a reference to the object in the string constant pool. If it exists, return the reference to the variable. Otherwise, add the reference and return it to the variable.

For details, please refer to the string constant pool in Java

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template