== 与equals 为什么用==比较两个对象的时候是false用equals比较的时候是true 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ==比较的是对象的内存地址。equals比较的是对象的数值。自然不一样了。建议比较数值的时候用equals 原始数据类型使用 == 判等引用类型override equals 判等 import java.util.*;public class EqualsTest{ public static void main(String args[]){ Employee alice1 = new Employee("Alice Adams",75000,1987,12,15); Employee alice2 = alice1; Employee alice3 = new Employee("Alice Adams",75000,1987,12,15); Employee bob = new Employee("Bob Brandson",50000,1989,10,1); //System.out.println(alice1); //System.out.println(alice2); //System.out.println(alice3); System.out.println("alice1 == alice2 : " +(alice1 == alice2)); System.out.println("alice1 == alice3 : " +(alice1 == alice3)); System.out.println("alice1.equals(alice3) : " +(alice1.equals(alice3))); System.out.println("alice1.equals(bob) : " +(alice1.equals(bob))); System.out.println("bob.toString: " + bob); Manager car1 = new Manager("Carl Cracker",80000,1987,12,15); Manager boss = new Manager("Carl Cracker",80000,1987,12,15); boss.setBonus(5000); System.out.println("boss.toString(): "+ boss); System.out.println("car1.equals(boss): "+ car1.equals(boss)); System.out.println("alice1.hashCode(): "+ alice1.hashCode()); System.out.println("alice3.hashCode(): "+ alice3.hashCode()); System.out.println("bob.hashCode(): "+ bob.hashCode()); System.out.println("car1.hashCode(): "+ car1.hashCode()); }}class Employee{ private String name; private double salary; private Date hireDay; public Employee(String n,double s,int year,int month,int day){ this.name = n; this.salary = s; GregorianCalendar calendar = new GregorianCalendar(year,month-1,day); this.hireDay = calendar.getTime(); } public String getName(){ return name; } public double getSalary(){ return salary; } public Date getHireDay(){ return hireDay; } public void raiseSalary(double byPercent){ double raise = salary * byPercent/100; salary +=raise; } public boolean equals(Object otherObject){ if(this == otherObject) { //System.out.println("---------------------------------"); //System.out.println(this.getClass().getName()); //System.out.println(this); //System.out.println(otherObject.getClass().getName()); //System.out.println(otherObject); //System.out.println("---------------------------------"); return true; } if(otherObject == null) return false; if(this.getClass() != otherObject.getClass()) return false; Employee other = (Employee)otherObject; return this.name.equals(other.name) && this.salary == other.salary && this.hireDay.equals(other.hireDay); } public int hashCode(){ return 7*this.name.hashCode() + 11 * new Double(salary).hashCode() + 13 * this.hireDay.hashCode(); } public String toString(){ return this.getClass().getName() + "[name=" + this.name + ",salary="+ this.salary + ",hireDay=" + this.hireDay +"]"; }};class Manager extends Employee{ private double bonus; public Manager(String n,double s,int year,int month,int day){ super(n,s,year,month,day); bonus = 0; } public double getSalary(){ double baseSalary = super.getSalary(); return baseSalary + bonus; } public void setBonus(double b){ this.bonus = b; } public boolean equals(Object otherObject){ if(!super.equals(otherObject)) return false; //调用父类中的equals方法 Manager other = (Manager)otherObject; return this.bonus == other.bonus; } public int hashCode(){ return super.hashCode() + 17 * new Double(bonus).hashCode(); } public String toString(){ return super.toString() + "[bonus=" + this.bonus+"]"; }}alice1 alice2 alice3 这三个对象的内存地址也是相同的 为什么 用==比较是false 用equals是true 有点不明白了 ==是比较两个对象的引用equals是比较两个对象的值 如果是引用数据类型的话,==比较的是对象的地址。equals()比较的是内容。其实equals()是Object类的一个方法,而所有类都是Object子类,所有类都有equals()这个方法,你可以重写这个方法完成你的比较。String类已经重写过equals()方法,直接可以用来比较String对象内容 alice1 alice2 相同 alice3 不同 lz.首先你的说法就是错误的。alice1 alice2 alice3 这三个对象的内存地址也是相同的 为什么 用==比较是false 用equals是true 有点不明白了你的alice1和alice2是一样的。alice1和alice2读取的是同一个内存地址。而alice和其他两个值是一样的,但是却是另外new出来的对象,只要new 出来之后,内存地址就不一样的了。多看看堆栈就明白了 alice1 alice2 相同 alice3 bob 不相同 楼主检查一下,是不是自己看错了,如果你就需要验证==和equals()你可以弄个简单的例子,试试就可以。引用实际上就像地址。你可以用System.identityHashCode(Object o)方法求出其特殊HashCode值,如果是不同对象其HashCode码是不同的,无论你怎样重写Object的HashCode方法,System.identityHashCode(Object o)方法都可以辨别出不同对象,也就是说此方法是根据地址求出的HashCode码,楼主可以拿这个方法测试下。 Employee@2cd55b63Employee@2cd55b63Employee@2cd55b63这是我打印出来的内存地址分别是alice1 alice2 alice3的 恩 的确1和2的相同 3的不同 但是用System.out.println(alcie1);System.out.println(alcie2);System.out.println(alcie3);打出来的结果是这样的Employee@2cd55b63Employee@2cd55b63Employee@2cd55b63 你那是输出的值。值自然是相同的了。System.out.println(System.identityHashCode(alice3));System.out.println(System.identityHashCode(alice1));你这样来判断他们的内存地址,输出就不一样了。 Employee@2cd55b63Employee@2cd55b63Employee@2cd55b63那输出来的这些是什么东西了 难懂不是地址码 @之前是类名称,之后是HashCode值,你看看你定义类的时候是不是重写了HashCode()方法!最好使用System.identityHashCode(Object o)来判断不同对象~因为这个方法就是根据地址求出的HashCode值,不同的对象绝对值不同。刚才12楼的兄台已经说得很明白了 获取短信猫中的SIM卡的号码以及具体信息 org.netbeans.lib这个包为什么在Netbeans安装目录找不到呢??? java中如何做一个可动的时间 关于字符串获取问题。 java 中如何判断当前的文件是否正在被别人使用中 怪了,用jbuilder2005打包生成的exe文件就是不能执行,以前用jbx打包就可以 在JTextArea上的滚动条如何加 Java操作Access数据库? jdk设置问题,请指教! 各位好,下面是一个最简单的程序,可出错了! 为什么float c;c=1.0/8;错误? eclipse的使用
equals比较的是对象的数值。
自然不一样了。建议比较数值的时候用equals
引用类型override equals 判等
import java.util.*;public class EqualsTest
{
public static void main(String args[]){
Employee alice1 = new Employee("Alice Adams",75000,1987,12,15);
Employee alice2 = alice1;
Employee alice3 = new Employee("Alice Adams",75000,1987,12,15);
Employee bob = new Employee("Bob Brandson",50000,1989,10,1);
//System.out.println(alice1);
//System.out.println(alice2);
//System.out.println(alice3);
System.out.println("alice1 == alice2 : " +(alice1 == alice2)); System.out.println("alice1 == alice3 : " +(alice1 == alice3)); System.out.println("alice1.equals(alice3) : " +(alice1.equals(alice3))); System.out.println("alice1.equals(bob) : " +(alice1.equals(bob))); System.out.println("bob.toString: " + bob); Manager car1 = new Manager("Carl Cracker",80000,1987,12,15); Manager boss = new Manager("Carl Cracker",80000,1987,12,15);
boss.setBonus(5000);
System.out.println("boss.toString(): "+ boss);
System.out.println("car1.equals(boss): "+ car1.equals(boss)); System.out.println("alice1.hashCode(): "+ alice1.hashCode());
System.out.println("alice3.hashCode(): "+ alice3.hashCode());
System.out.println("bob.hashCode(): "+ bob.hashCode());
System.out.println("car1.hashCode(): "+ car1.hashCode()); }
}
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n,double s,int year,int month,int day){
this.name = n;
this.salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,month-1,day);
this.hireDay = calendar.getTime();
} public String getName(){
return name;
}
public double getSalary(){
return salary;
} public Date getHireDay(){
return hireDay;
} public void raiseSalary(double byPercent){
double raise = salary * byPercent/100;
salary +=raise;
} public boolean equals(Object otherObject){
if(this == otherObject) {
//System.out.println("---------------------------------");
//System.out.println(this.getClass().getName());
//System.out.println(this);
//System.out.println(otherObject.getClass().getName());
//System.out.println(otherObject);
//System.out.println("---------------------------------"); return true;
} if(otherObject == null) return false; if(this.getClass() != otherObject.getClass()) return false; Employee other = (Employee)otherObject; return this.name.equals(other.name) && this.salary == other.salary && this.hireDay.equals(other.hireDay); } public int hashCode(){
return 7*this.name.hashCode() + 11 * new Double(salary).hashCode() + 13 * this.hireDay.hashCode();
} public String toString(){
return this.getClass().getName() + "[name=" + this.name + ",salary="+ this.salary + ",hireDay=" + this.hireDay +"]";
}
};
class Manager extends Employee
{
private double bonus;
public Manager(String n,double s,int year,int month,int day){
super(n,s,year,month,day);
bonus = 0;
} public double getSalary(){
double baseSalary = super.getSalary();
return baseSalary + bonus;
} public void setBonus(double b){
this.bonus = b;
} public boolean equals(Object otherObject){
if(!super.equals(otherObject)) return false; //调用父类中的equals方法
Manager other = (Manager)otherObject;
return this.bonus == other.bonus;
} public int hashCode(){
return super.hashCode() + 17 * new Double(bonus).hashCode();
} public String toString(){
return super.toString() + "[bonus=" + this.bonus+"]";
}
}alice1 alice2 alice3 这三个对象的内存地址也是相同的 为什么 用==比较是false 用equals是true 有点不明白了
alice3 不同
alice1 alice2 alice3 这三个对象的内存地址也是相同的 为什么 用==比较是false 用equals是true 有点不明白了
你的alice1和alice2是一样的。alice1和alice2读取的是同一个内存地址。而alice和其他两个值是一样的,但是却是另外new出来的对象,只要new 出来之后,内存地址就不一样的了。多看看堆栈就明白了
alice3 bob 不相同
楼主检查一下,是不是自己看错了,如果你就需要验证==和equals()你可以弄个简单的例子,试试就可以。引用实际上就像地址。你可以用System.identityHashCode(Object o)方法求出其特殊HashCode值,如果是不同对象其HashCode码是不同的,无论你怎样重写Object的HashCode方法,System.identityHashCode(Object o)方法都可以辨别出不同对象,也就是说此方法是根据地址求出的HashCode码,楼主可以拿这个方法测试下。
Employee@2cd55b63
Employee@2cd55b63
Employee@2cd55b63
这是我打印出来的内存地址分别是alice1 alice2 alice3的
但是用System.out.println(alcie1);
System.out.println(alcie2);
System.out.println(alcie3);打出来的结果是这样的Employee@2cd55b63
Employee@2cd55b63
Employee@2cd55b63
你那是输出的值。值自然是相同的了。
System.out.println(System.identityHashCode(alice3));
System.out.println(System.identityHashCode(alice1));
你这样来判断他们的内存地址,输出就不一样了。
Employee@2cd55b63
Employee@2cd55b63
那输出来的这些是什么东西了 难懂不是地址码