Java equals()方法的使用

Java equals()方法的使用,java,Java,所以我不确定为什么下面的代码返回它们不相等。从检查结果来看,他们应该是平等的。有人能帮我吗?先谢谢你 public class BenAndLiam { public static void main(String[] args){ String[] name = new String[2]; name[0] = "Liam"; name[1] = "Short"; int[] marks = new int[3]; marks[0] = 90;

所以我不确定为什么下面的代码返回它们不相等。从检查结果来看,他们应该是平等的。有人能帮我吗?先谢谢你

public class BenAndLiam {
public static void main(String[] args){
    String[] name = new String[2];
    name[0] = "Liam";
    name[1] = "Short";
    int[] marks = new int[3];
    marks[0] = 90;
    marks[1] = 50;
    marks[2] = 70;

    //make students
    Student Liam = new Student(1111, name, marks);
    Student Ben = new Student(1111, name, marks);

    //print Liam's info
    System.out.println(Liam.getId() + " " + Liam.getName()[0] + " " + 
    Liam.getName()[1] + " " + Liam.getMarks()[0] + " " + Liam.getMarks()[1] +
    " " + Liam.getMarks()[2]);
    System.out.println(Ben.getId() + " " + Ben.getName()[0] + " " + 
            Ben.getName()[1] + " " + Ben.getMarks()[0] + " " + Ben.getMarks()[1] +
            " " + Ben.getMarks()[2]);

    //check for equality
    if(Ben.equals(Liam))
        System.out.println("They're equal");
    else System.out.println("They're not equal");
    }
}
我的学生代码:

public class Student {
//The aspects of a student
private int id;
private String name[];
private int marks[];


//Constructor 1

public Student(int id, String name[]){
    this.id = id;
    this.name = name;
}

//Constructor 2
public Student(int id, String name[], int marks[]){
    setId(id);
    setName(name);
    setMarks(marks);
}

//accessor for id
public int getId(){
    return id;
}

//accessor for name
public String getName()[]{
    return name;
}

//accessor for marks
public int getMarks()[]{
    return marks;
}

//Mutator for id
public void setId(int id){
    this.id = id;
}
//mutator for name
public void setName(String name[]){
    this.name = name;
}
//Mutator for marks
public void setMarks(int marks[]){
    this.marks = marks;
}
}

从表面上看,我需要在我的学生课堂上使用平等吗

更新: 我只是通过在我的学生课堂上添加以下代码来实现:

public boolean equals(Student otherstudent){
    return ((id == otherstudent.id) && (name.equals(otherstudent.name)
            && (marks == otherstudent.marks)));
}

干杯,伙计们

操作符==,测试两个对象引用变量是否引用了一个对象的完全相同的实例


方法,.equals测试两个相互比较的对象是否相等,但它们不必是同一对象的完全相同实例

Student Liam = new Student(1111, name, marks);
Student Ben = new Student(1111, name, marks);
在上面的代码中,Liam==Ben为false,因为尽管它们都有值1111、name、marks,但它们是两个不同的对象

此外,在上面的代码中,Liam.equalsBen是真的,因为尽管它们是两个不同的对象,但它们是等价的,因为它们表示相同的值。

您应该重写学生类中的equals方法

<>请阅读:

如果两个引用指向同一个对象,java将考虑相等。

Student Liam = new Student(1111, name, marks);
Student Ben = new Student(1111, name, marks);
要比较字符串,可以使用方法equalsIgnoreCase from String class。

默认情况下,Object.equals检查两个变量是否指向同一个对象-同一内存空间中的一个实例。您有两个具有相同数据的不同对象。您需要重写.equals方法来相互比较对象的内容,而不是比较内存地址的默认方法


如果您确实重写了equals,请确保也重写了hashCode。请阅读对象javadoc中两种方法的约定,并确保遵循该约定,否则您的程序可能会出现错误行为,尤其是在使用Collections API时。

obj1.equalsobj2在obj2引用的对象与obj1相同时始终返回true

    Student obj1=new Student();
    Student obj2= new Student();

    obj1.setId(45);
    obj1.setName("obj1 Name");
    obj1.setFatherName("obj1's father");

    obj2.setId(45);
    obj2.setName("obj1 Name");
    obj2.setFatherName("obj1's father");

    System.out.println(obj1.equals(obj2));
将始终返回false,因为obj1和obj2是类Student的新对象,尽管这两个对象的值相同,但内存引用不同

与我的上述声明相反

    Student obj1=new Student();
    Student obj2= obj1;

    obj1.setId(45);
    obj1.setName("obj1 Name");
    obj1.setFatherName("obj1's father");

    obj2.setId(46);
    obj2.setName("obj2 Name");
    obj2.setFatherName("obj2's father");

    System.out.println(obj1.equals(obj2));

将返回true,因为obj2实际上引用的是obj1的对象,尽管在这种情况下,值不同。因此,没有为obj2分配新的内存位置,因此它将返回true。

能否显示student的equals方法?你应该重写这个方法并在那里处理你的逻辑。为学生发布你的代码。您是否定义了一个equals?您是否记得在学生类中重写equals和hashCode?我刚刚为学生添加了代码。看我原来的帖子。我让它工作了!我没有描述的是,在我的学生课堂上,正确的术语是我的方法。这是我在学生类中输入的代码:public boolean equalstudent otherstudent{return id==otherstudent.id&&name.equalstudent.name&&marks==otherstudent.marks;}干杯!不,默认情况下它会检查引用相等。方法,.equals会测试两个相互比较的对象是否相等,但它们不必是同一对象的完全相同的实例。这不会改变默认操作是引用相等的事实,与此答案的原始版本不同,字符串值不相等。您的答案仍然是错误的,或者更确切地说,是误导性的:在为学生实现equals之前,Liam==Ben==Liam.equalsBen==false.+1和hashCode是一致的。您可以使用IDE生成这些。