设为首页 加入收藏

TOP

String 常量池和 String#intern()(一)
2017-12-30 06:06:50 】 浏览:470
Tags:String 常量 池和 String#intern

String是Java基础的重要考点。可问的点多,而且很多点可以横向切到其他考点,或纵向深入JVM。

本文略过了String的基本内容,重点在于String#intern()。

String常量池

String常量可能会在两种时机进入常量池:

  1. 编译期:通过双引号声明的常量(包括显示声明静态编译优化后的常量,如”1”+”2”优化为常量”12”),在前端编译期将被静态的写入class文件中的“常量池”。该“常量池”会在类加载后被载入“内存中的常量池”,也就是我们平时所说的常量池。同时,JIT优化也可能产生类似的常量。
  • 运行期:调用String#intern()方法,可能将该String对象动态的写入上述“内存中常量池”。

时机1的行为是明确的。原理可阅读class文件结构、类加载、编译期即运行期优化等内容。

时机2在jdk6和jdk7中的行为不同,下面讨论。

String#intern()

读者可直接阅读参考资料。下述总结仅为了猴子自己复习方便。

声明

/** 
 * Returns a canonical representation for the string object. 
 * <p> 
 * A pool of strings, initially empty, is maintained privately by the 
 * class <code>String</code>. 
 * <p> 
 * When the intern method is invoked, if the pool already contains a 
 * string equal to this <code>String</code> object as determined by 
 * the {@link #equals(Object)} method, then the string from the pool is 
 * returned. Otherwise, this <code>String</code> object is added to the 
 * pool and a reference to this <code>String</code> object is returned. 
 * <p> 
 * It follows that for any two strings <code>s</code> and <code>t</code>, 
 * <code>s.intern() == t.intern()</code> is <code>true</code> 
 * if and only if <code>s.equals(t)</code> is <code>true</code>. 
 * <p> 
 * All literal strings and string-valued constant expressions are 
 * interned. String literals are defined in section 3.10.5 of the 
 * <cite>The Java? Language Specification</cite>. 
 * 
 * @return  a string that has the same contents as this string, but is 
 *          guaranteed to be from a pool of unique strings. 
 */  
public native String intern();

String#intern()是一个native方法。根据Javadoc,如果常量池中存在当前字符串, 就会直接返回当前字符串. 如果常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回。

实现原理

JNI最后调用了c++实现的StringTable::intern()方法:

oop StringTable::intern(Handle string_or_null, jchar* name,  
                        int len, TRAPS) {  
  unsigned int hashValue = java_lang_String::hash_string(name, len);  
  int index = the_table()->hash_to_index(hashValue);  
  oop string = the_table()->lookup(index, name, len, hashValue);  
  // Found  
  if (string != NULL) return string;  
  // Otherwise, add to symbol to table  
  return the_table()->basic_add(index, string_or_null, name, len,  
                                hashValue, CHECK_NULL);  
}
oop StringTable::lookup(int index, jchar* name,  
                        int len, unsigned int hash) {  
  for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) {  
    if (l->hash() == hash) {  
      if (java_lang_String::equals(l->literal(), name, len)) {  
        return l->literal();  
      }  
    }  
  }  
  return NULL;  
}

在the_table()返回的hash表中查找字符串,如果存在就返回,否则加入表。

StringTable是一个固定大小的Hashtable,默认大小是1009。基本逻辑与Java中HashMap相同,也使用拉链法解决碰撞问题。

既然是拉链法,那么如果放进的String非常多,就会加剧碰撞,导致链表非常长。最坏情况下,String#intern()的性能由O(1)退化到O(n)。

  • jdk6中StringTable的长度固定为1009。
  • jdk7中,StringTable的长度可以通过一个参数-XX:StringTableSize指定,默认1009。

jdk6和jdk7下String#intern()的区别

引言

相信很多Java程序员都做类似String s = new String("abc");这个语句创建了几个对象的题目。这种题目主要是为了考察程序员对字符串对象常量池的掌握。上述的语句中创建了2个对象:

  • 第一个对象,内容”abc”,存储在常量池中。
  • 第二个对象,内容”abc”,存储在堆中。

问题

来看一段代码:

public static void main(String[] args) {
    String s = new String("1");
    s.intern();
    String s2 = "1";
    System.out.println(s == s2)
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇大型网站系统与 Java 中间件实践 下一篇代码生成利器:IDEA 强大的 Live ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目