看源码,加深理解---(零)String#equals(Object)
2017-09-06 07:31
393 查看
零,写在最前面
看源码的目的在于通过源码了解java是如何工作的,有哪些地方可以借鉴一,首先附上源码
/** * Compares this string to the specified object. The result is {@code * true} if and only if the argument is not {@code null} and is a {@code * String} object that represents the same sequence of characters as this * object. * * @param anObject * The object to compare this {@code String} against * * @return {@code true} if the given object represents a {@code String} * equivalent to this string, {@code false} otherwise * * @see #compareTo(String) * @see #equalsIgnoreCase(String) */ public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
二,然后看一下源码上面的注释
/**java * Compares this string to the specified object. The result is {@code * true} if and only if the argument is not {@code null} and is a {@code * String} object that represents the same sequence of characters as this * object. * * @param anObject * The object to compare this {@code String} against * * @return {@code true} if the given object represents a {@code String} * equivalent to this string, {@code false} otherwise * * @see #compareTo(String) * @see #equalsIgnoreCase(String) */
注释的内容如下:
将此字符串和指定对象进行比较,结果为真的的情况有且只有参数不为空且为字符串的对象并且与此相同字符序列的对象.
简单的说就是:
此字符串和指定对象进行比较,只有这个对象不为空,是字符串并且具有与此字符串相同的字符串序列才会返回真.
三,看一下源码
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
解释如下:
首先,方法的返回值为布尔类型,获取的参数为Object类型.方法的第一步,首先比较字符串和对象的地址是否相同,相同的话直接返回真,如果不是同一个对象,判断对象是不是String类的子类,如果是,将对象的类型强转为String,并赋值给anotherString引用.定义一个整数型的n来存储value的长度,这里涉及到一个成员变量
/** The value is used for character storage. */private final char value[];`即该值用于字符存储.然后如果该字符串与指定对象的长度相同,则进入if判断中,创建一个char类型的数组v1来保存字符串的结果,创建一个char类型的数组v2来保存指定对象的结果.定义一个int姓的对象i赋初值为1.用while循环遍历v1和v2的值,对比v1和v2中相同位置的字节,只要有一处不能对应则返回假.
相关文章推荐
- Map<String, Object> map = new HashMap<String, Object>();的理解
- SerializationUtils&RandomUtils&RandomStringUtils&ObjectUtils 源码研究
- [Lua源码阅读] 理解Lua的object
- 从prototype源码总结的一些浏览器的“能力”( Object.prototype.toString.call)
- Object改写之前和改写之后(如String和tostring)的==和equals的关系
- JDK源码之解读String最终类的equals()和hashcode()方法实现原理
- 解决Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null···
- 黑马程序员【阅读源码理解String对象的不变性】
- JDK源码之解读String最终类的equals()和hashcode()方法实现原理
- C#基础之值类型和引用类型的== 与 Equals,string和object的特殊性
- Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec
- 从Java源码角度彻底理解String,StringBuilder和StringBuffer的区别
- Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec
- Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec
- C#中关于String.Equals(object,object)和(object==object )的比较
- C#中关于String.Equals(object,object)和(object==object )的比较
- java 通过源码看String的Equals与==
- jdk源码初体验之String的equals
- Java基础(11):字符串—深入理解String对象和比较符==和equals的不同
- String有重写Object的hashcode和toString吗?重写equals不重写hashCode会有什么问题?