第一关
package bayuejiu;
public class equals {
public static void main(String[] args) {
String name1 = "Tom";
String name2 = "Tom";//先检查常量池中,有的话name2直接指向该空间
System.out.println(name1==name2);//true 地址相同
System.out.println(name1.equals(name2));//思考这是怎么执行的
}
}
源码如下
public boolean equals(Object anObject) {//anObject指name2,是上转型对象
if (this == anObject) {//this指调用此方法(equals)的对象name1,name1和name2地址相同,返回true
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;
}
先判断地址是否相等,再比对字符串内容
第二关
package bayuejiu;
public class equals {
public static void main(String[] args) {
String name1 = "Tom";
String name2 = new String("Tom");//JVM在堆中直接创建的对象
System.out.println(name1==name2);//false 地址不同
System.out.println(name1.equals(name2));//思考这有什么不同了
}
}
再看源码:
public boolean equals(Object anObject) {//name2是上转型对象
if (this == anObject) {//name1与name2地址不同,向下执行
return true;
}
if (anObject instanceof String) {//name2是字符串的子类
String anotherString = (String)anObject;
int n = value.length;//value是全局变量,这里隐藏了this,实则this.value.length,即name2的字符个数
if (n == anotherString.value.length) {//先判断两字符串长度是否相同
char v1[] = value;//name1字符组成的数组
char v2[] = anotherString.value;//name2字符组成的数组
int i = 0;
while (n-- != 0) {//再逐一比对对应字符
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
第三关
package bayuejiu;
public class equals {
public static void main(String[] args) {
String name1 = "Tom";
Test test = new Test();
System.out.println(name1 == test);//javac编译时判断出两者类型不相同故通不过编译
}
}
package bayuejiu;
public class equals {
public static void main(String[] args) {
String name1 = "Tom";
Test test = new Test();
System.out.println(name1.equals(test));//思考这又是。。
}
}
源码:
public boolean equals(Object anObject) {//test是上转型对象
if (this == anObject) {
return true;
}
if (anObject instanceof String) {//false
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;
}
第四关
import java.util.ArrayList;
public class contain {
public static void main(String[] args) {
ArrayList<String> nameList = new ArrayList<>();//在JDK7之后后面的尖括号可省略泛型
nameList.add("Tom");
String name = "Tom";
System.out.println(nameList.contains(name));
}
}
源码:
public boolean contains(Object o) {//o是上转型对象name
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))//o调用的是String类的equals方法,和容器中的对象逐一比对
return i;
}
return -1;
}
第五关
import java.util.ArrayList;
public class contain {
public static void main(String[] args) {
ArrayList<String> nameList = new ArrayList<>();
nameList.add("Tom");
String name = null;
System.out.println(nameList.contains(name));
}
}
源码:
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)//size是全局变量,实为this.size,表示容器元素个数
if (elementData[i]==null)//这里的对比规则不是调用equals方法,而是等号==
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
第六关
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> nameList = new ArrayList<>();
nameList.add("Tom");
Test test = new Test();
System.out.println(nameList.contains(test));//思考这次该是。。。
}
}
源码:
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
//调用test的equals方法,而Test类中并无重写Objecte的quals方法,故这里调用的是继承自父类Object的equals方法
return i;
}
return -1;
}