String用法详解(equal源码 ==和equal的解释、字面赋值和new赋值效率、重写了hashcode的方法解释)
2011-04-17 18:10
627 查看
String a = “abc”;//在字符串池中找abc,如果有,就直接返回地址,如果没有就加值abc然后再返回地址(此方式的值是存放在字符串池中)String b = “abc”;String c = new String("abc");//在字符串池中找abc,如果有,就不管,如果没有就在对象池中加个abc,然后在堆中建一个对象abc,返回地址(此方式值存放在堆中的)String d = new String("abc");a ==b truea.equal(b) trueb==c false;b.equal(c) true;c==d false;c.equal(d) true;equals方法说明如果是String对象就调用string的equals,就比较他的值如果是其他对象,默认调用Object的equals,比较是不是同一个引用和==相同,如果是其他对象,而且自己实现了equals方式,就按照他自己的equals对象的比较方式。Object equal方法的源代码public boolean equals(Object obj) {return
(this == obj);//判断调用对象和传入对象的地址是不是一样的}Stirng
的
equal
方法的源代码public boolean equals(Object anObject) {//自己跟自己比if (this == anObject) {return true;}if
(anObject instanceof String) {//判断是
String
的实例String anotherString = (String)anObject;//强制转化
,
以防父类用子类的方法用不到//一个字符一个字符的比较int n = count;if (n == anotherString.count) {char
v1[] = value;char
v2[] = anotherString.value;int
i = offset;int
j = anotherString.offset;while
(n-- != 0) {if (v1[i++] != v2[j++])return
false;}return
true;}}return
false;}
(this == obj);//判断调用对象和传入对象的地址是不是一样的}Stirng
的
equal
方法的源代码public boolean equals(Object anObject) {//自己跟自己比if (this == anObject) {return true;}if
(anObject instanceof String) {//判断是
String
的实例String anotherString = (String)anObject;//强制转化
,
以防父类用子类的方法用不到//一个字符一个字符的比较int n = count;if (n == anotherString.count) {char
v1[] = value;char
v2[] = anotherString.value;int
i = offset;int
j = anotherString.offset;while
(n-- != 0) {if (v1[i++] != v2[j++])return
false;}return
true;}}return
false;}
java String 的字面赋值方式 public String intern() 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 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. 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. All literal(字面) strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification Returns: a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings. Lexical Structure >Literals >String Literals The Java Language Specification (所有虚拟机都必须要实现的东西) 字符串池里面是不会丢的, 而堆里面是会被回收的,使用字面建string效率会高. string的hashcode的源码 /** * Returns a hash code for this string. The hash code for a * <code>String</code> object is computed as * <blockquote><pre> * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre></blockquote> * using <code>int</code> arithmetic, where <code>s[i]</code> is the * <i>i</i>th character of the string, <code>n</code> is the length of * the string, and <code>^</code> indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ //就是按s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]公式来生成int序列 public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }
相关文章推荐
- List contains 方法无效 -> 重写 HashCode和Equal方法
- JDK源码之解读String最终类的equals()和hashcode()方法实现原理
- 重写equals时还必须重写hashcode方法,详解
- delphi中VirtualStringTree树使用方法之终结篇!2011-07-01 14:06VirtualStringTree 的用法详解:
- 重写equal和hashCode方法,用集合去重对象
- 重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源
- 详解重写equals()方法就必须重写hashCode()方法的原因
- 重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源
- 重写equels还要重写hashcode方法的解释
- Java编程:String 类中 hashCode() 方法详解
- 使用hashmap存储对象 并获取元素 祛除重复元素需要在类对象中重写hashcode和equal方法哦
- JDK源码学习之重写hashCode()和equals()方法
- Js中String对象方法replace()用法详解
- java中重写equals()方法的同时要重写hashcode()方法(详解)
- 重写equal()为什么需要重写hashCode方法
- 深入详解SetHash的元素为什么要重写hashCode和equals方法
- 源码剖析HashMap--当key为Object时为什么要重写hashcode与equals方法
- set实体泛型对象去重(重写实体hashCode、equal方法)
- 重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源
- 重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源