- 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;
- }
复制代码
以上是Sting中equals方法源码 |