-4

So I'm not sure why the following code returns "They're not equal". From inspection it should return "They're equal". Can anyone help me out? Thank you in advance.

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");
    }
}

My code for student:

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;
}

}

By the looks of things I need to have some sort of heading for the use of equals() in my Student class?

UPDATE: I just got it working by adding this code into my Student class:

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

Cheers guys!

Liam Shaw
  • 5
  • 2

5 Answers5

7

You should override the equals() method in Student class.

Please prefer to read :What issues should be considered when overriding equals and hashCode in Java?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
5

By default, Object.equals() checks if two variables point to the same object - one instance in the same memory space. You have two different objects with the same data. You need to override the .equals() method to compare the contents of the objects to each other instead of the default, which compares memory addresses.

If you do override equals(), make sure to also override hashCode(). Read the contracts for both methods in the Object javadoc and make sure you follow it, or your programs will probably misbehave, especially if you use the Collections API.

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
Eric Stein
  • 13,209
  • 3
  • 37
  • 52
0

obj1.equals(obj2) always returns true when obj2 is referencing the same object that is obj1.

    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));

will always return false because obj1 and obj2 are new objects of a Class Student, although the values of both the objects are same but, there memory references are different.

In Contrast to my above statement,

    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));

will return true because the obj2 is actually referring the object of obj1 although in this scenario the values are different. So, no new memory location allotted to obj2 and hence it will return true.

-1

Java will consider Equals if two references point to same object.

To compare strings, you can use the method equalsIgnoreCase() from String class.

guisantogui
  • 4,017
  • 9
  • 49
  • 93
-5

The operator, ==, tests to see if two object reference variables refer to the exact same instance of an object.

The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object.

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

In the above code, Liam == Ben is false because, although they both have the value 1111, name, marks, they are two different objects.

Also, in the above code, Liam .equals(Ben) is true because although they are two different objects, they are equivalent in the fact that they represent the same value.

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • No, it checks reference equality by default. – mikołak Aug 21 '13 at 11:03
  • The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object. – Mohammod Hossain Aug 21 '13 at 11:15
  • That doesn't change the fact the **default** operation is reference equality, not "string values equality" as in the original revision of this answer. And your answer is still wrong, or rather, misleading: until `equals()` is implemented for `Student`, `((Liam == Ben) == Liam.equals(Ben)) == false`. – mikołak Aug 21 '13 at 11:26