-2

The code is as following:

public class Main {
    public static void main(String[] args) {
        Student a = new Student(10, "Ole");
        Student b = new Student(10, "Ole");

        System.out.println(a.hashCode());
        System.out.println(b.hashCode());
    }
}

and the object looks like this:

public class Student {
    private int snr;
    private String namn;

    public Student(int snr, String namn) {
        this.snr = snr;
        this.namn = namn;
    }
}

These are the results when running the code:

57651960
441366923

I've read some of the javadoc, but I can't find out why, can anyone explain why this happens? And also what I would have to do to make the result identical? (if at all possible)

Thanks for the explanations :)! Makes sense now ;)

Patidati
  • 1,048
  • 2
  • 12
  • 19

3 Answers3

6

You are not overriding Object.hashCode(), so it's returning a value based on the object identity.

From the documentation:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

sp00m
  • 47,968
  • 31
  • 142
  • 252
robertoia
  • 2,301
  • 23
  • 29
2

It is the responsibility of the Student class to provide a proper equals() method which is consistent with the hashCode() contract. From the JavaDoc:

If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.

And here the full example:

class Student {

    private int snr;
    private String name;

    public Student(int snr, String name) {
        this.snr = snr;
        this.name = name;
    }

    @Override public boolean equals(Object obj) {
        if (!(obj instanceof Student)) return false;
        Student other = (Student) obj;
        return Objects.equals(snr, other.snr) && Objects.equals(name, other.name);
    }

    @Override public int hashCode() {
        return Objects.hash(snr, name);
    }
}
Harmlezz
  • 7,972
  • 27
  • 35
  • So if I were to create a equals() method within the Student class that shows that they are equal, the value would be the same? Or am I misunderstanding? – Patidati Apr 28 '14 at 13:44
  • 2
    You need to implement when to consider two `Students` to be equal. And if they are considered to be equal, they have to return the same hash code. The contract is a bit more complex, but the aspects pointed out by me are mandatory to implement. Read the JavaDoc of both methods carefully.You will have to provide an implementation for those many times in your career. – Harmlezz Apr 28 '14 at 13:48
  • A few people have the same name as me. Does this have an impact on whether they are the same person as me. Clearly not, and so name should not be part of the `equals`/`hashCode` implementation. Probably it does not make sense to override `equals` here. – Tom Hawtin - tackline Apr 28 '14 at 14:34
1

You have not overwritten the underlying Object.Hashcode, you would need something like this, your ide can generate hashcode and equals for you.

public class Student {
    private int snr;
    private String namn;

    public Student(int snr, String namn) {
        this.snr = snr;
        this.namn = namn;
    }

@override
public int hashcode(){
    int result = super.hashCode();
    result = 31 * result + (namn!= null ? namn.hashCode() : 0) + snr;
    return result;
  } 
}
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311