比如我创建了一个String对象,且此时“abcd”字面量并不在常量池中。
String s = new String("abcd");
疑问1:在堆上创建String对象时,常量池里会立即创建一个"abcd"吗?
如果疑问1为是,我看到String.intern()方法的说明如下:
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.
疑问2:既然堆上创建的String立即将字面量存入常量池,怎样的String对象才能符合上文"Otherwise"的条件?
如果疑问1为否,
String s = new String("abcd");
疑问3:反编译上述代码,里面有一个ldc指令,我查了下是立即将String从常量池推送入栈顶,也就是说"abcd"早就伴随s的创建存在于常量池里?
------解决思路----------------------
貌似不会...
好像new的是不会加进去的
只是new String("abcd")里写"abcd"时候本来就相当于已经在常量池里面放了个
纯误导..等大神~