I override both the hashCode() and equals(), but I don't modify anything inside the overridden methods.
@Override public int hashCode() { int hash = 7; hash = 67 * hash + Objects.hashCode(this.model); hash = 67 * hash + this.year; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final PC other = (PC) obj; if (!Objects.equals(this.model, other.model)) { return false; } if (this.year != other.year) { return false; } return true; }I create 2 identical objects:
PC One = new PC(); One.setModel("HP"); One.setYear(2013); PC Two = new PC(); Two.setModel("HP"); Two.setYear(2013);I compare those 2 objects:
if (One.equals(Two)) { System.out.println("They are the same objects!"); } else { System.out.println("They are different objects!"); }
The result is: "They are the same objects!". However, if I don't override both methods, the result will be: "They are different objects!". Because the hashCode is unique for each object (I suppose), I have expected thet the result to be: "They are different objects!". Q: Why?
PC?equalslook like?