为什么用==比较两个对象的时候是false
用equals比较的时候是true
用equals比较的时候是true
解决方案 »
- org.apache.commons
- 小女子求助,谁会写ComboBoxModel接口??
- 一个字符串题
- 多线程的问题一个
- applet如何自动检测客户端的jre版本是否是jre1.5,如果不是则提示下载!
- 页面的加了这个上传的enctype="multipart/form-data,怎么取字段的数组值,不能request.getParameterValues(""),急!!!
- 彩色验证码问题
- 关于在swing按钮button上加网页连接的问题??解决立即给分!!
- 关于JDBC的初等问题!小弟很着急呀!
- 怎么使网格布局管理器不是居中显示?再线等待
- 为什么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
那输出来的这些是什么东西了 难懂不是地址码