initially empty, is maintained privately by the * class {@code String}. * <p> * When the intern method is invoked, if the pool already contains a * string equal to this {@code String} object as
string);jchar*chars=java_lang_String::as_unicode_string(string,length,CHECK_NULL);oop result=intern(h_string,chars,length,CHECK_NULL);returnresult;}...oop StringTable::intern(Handle string_or_null_h,jchar*name,int len,TRAPS){...unsigned int hash=java_lang_String::hash_code(...
上面说过如果是使用引号声明的字符串都是会直接在字符串常量池中生成,而 new 出来的 String 对象是放在 JAVA Heap 区域。所以拿一个 JAVA Heap 区域的对象地址和字符串常量池的对象地址进行比较肯定是不相同的,即使调用String.intern方法也是没有任何关系的。 2.2 jdk7中的解释# 在jdk6 以及以前的版本中,字符串...
if (java_lang_String::equals(l->literal(), name, len)) { return l->literal(); } } } return NULL; } 它的大体实现结构就是: JAVA 使用 jni 调用c++实现的StringTable的intern方法, StringTable的intern方法跟Java中的HashMap的实现是差不多的, 只是不能自动扩容。默认大小是1009。 要注意的是,Str...
两个new String相加会被优化为StringBuilder,可以通过javac和javap查看汇编指令如下:javac InternTest.javajavap -c InternTest publicclasscom.justin.java.lang.InternTest{ publiccom.justin.java.lang.InternTest();Code:: aload_0 1: invokespecial #1// Method java/lang/Object."<init>":()V4:return...
java源码 /*** Returns a canonical representation for the string object. * * 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 ...
Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the classString. When the intern method is invoked, if the pool already contains a string equal to thisStringobject as determined by the#equals(Object)method, then the string...
简单来说,在Java8中,String类维护了一个字符串常量池(注意此常量池在运行期间位于堆中),当调用intern方法时,首先在常量池中查看是否已有相同的字符串(字符串是否相同使用String的equal方法判断),如果常量池中已有,则直接返回该字符串的引用,如果没有,则将当前字符串对象加入常量池中,并返回当前字符串的引用。
{@code String}. * * When the intern method is invoked, if the pool already contains a * string equal to this {@code String} object as determined by * the {@link #equals(Object)} method, then the string from the pool is * returned. Otherwise, this {@code String} object is added...
1,直接使用双引号声明出来的String对象会直接存储在常量池中。 2,使用new String(“xxxx”)来初始化。 两者的区别是直接声明的String对象是声明在常量池中的;而new初始化的对象是放在Java堆中的。 以下例子来说明问题: 代码1: static void test1(){