==运算符
可以使用在基本数据类型变量和引用数据类型变量中
//值类型比较
int i =10;
int j =10;
double d =10.0;
System.out.println(j==d);//true
System.out.println(i==j); //true
//引用类型比较
Customer c1 = new Customer("Tom", 21);
Customer c2 = new Customer("Tom", 21);
System.out.println(c1==c2);//false
equals
是一个方法而非运算符 只能适用于引用数据类型 值类型想使用的话得需要使用对应的包装类
equals
方法作用和==
相同
定义一个Customer类没有重写equals方法public class Customer {
private String name;
private int age;
public Customer() {
}
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试
Customer c1 = new Customer("Tom", 21);
Customer c2 = new Customer("Tom", 21);
System.out.println(c1==c2);//false
String s1= new String("zhaofuhao");
String s2 = new String("zhaofuhao");
System.out.println(s1==s2); //true
总结: