看源码,加深理解---(零)String#equals(Object)
来源:互联网 发布:c 遍历二维数组 编辑:程序博客网 时间:2024/05/20 06:31
零,写在最前面
看源码的目的在于通过源码了解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中相同位置的字节,只要有一处不能对应则返回假.
阅读全文
1 0
- 看源码,加深理解---(零)String#equals(Object)
- java 通过源码看String的Equals与==
- String equals源码分析
- String 中equals()源码
- String.equals()源码分析
- 理解Object转String
- 源码阅读—String equals()
- Object 和 String 的equals比较
- String的equals与Object的equals的区别
- Java中Object.equals与String.equals的区别
- Object中 hashCode ---equals的完全理解
- Object 九大方法之equals理解
- C#中关于String.Equals(object,object)和(object==object )的比较
- C#中关于String.Equals(object,object)和(object==object )的比较
- 源码分析—String equals方法
- String中equals的源码片段
- Java String类的equals方法源码
- Object.equals()
- java 基本类型和常量
- Codeforces Round #432 (Div. 2) D 850B Arpa and a list of numbers(gcd 枚举)
- 学生管理系统 c语言
- Effective C++ rule 21.尽量不要返回reference.
- 考研英语
- 看源码,加深理解---(零)String#equals(Object)
- MySQL 5.7 免安装版配置
- ConcurrentHashMap总结
- TensorFlow softmax VS sparse softmax
- HSV颜色空间中的肤色检测
- String与StringBuffer以及StringBuilder的异同点
- leetcode:561. Array Partition I
- 编码,UTF8,UNICODE
- OpenCV2编程手册笔记之 4.3查找表修改图像外观