翻阅String类源代码发现几个有趣的地方
1、看以下代码 Stirng类的hashcode 方法
/** * 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 §3.10.5 of the * <a href="http://java.sun.com/docs/books/jls/html/">Java Language * Specification</a> * * @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 在内部是缓存的。
才发现,其实臧圩人已经说得很清楚了 我就不多赘述了
"在JAVA虚拟机(JVM)中存在着一个字符串池,其中保存着很多String对象,并且可以被共享使用,因此它提高了效率。由于String类是final的,它的值一经创建就不可改变,因此我们不用担心String对象共享而带来程序的混乱。字符串池由String类维护,我们可以调用intern()方法来访问字符串池。"
参见
http://zangweiren.iteye.com/blog/216005 1 楼 抛出异常的爱 2009-05-07 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] *
这部分说明是这个string 的char数组的ascII拼接后31位进制的值。
不知道为啥不是32或30 貌似没什么区别。
如果算子是个质数的话
在hash算法时
数据更趋向正态分布
好像是说:
向一个hash树上挂时能更充分的使用空间,
不用挂一个就要扩一次.