Hullo!
I have this code
public int hashCode(){
int res = (int) (id ^ (id >>> 32));
res = 31 * res + (name != null ? name.hashCode() : 0);
return res;
}
And this is my equals code
public boolean equals(Object o) {
// object ref identical or not
if (this == o) return true;
if (o == null) return false;
//class identical?
if (getClass() != o.getClass()) return false;
Person person = (Person) o;
if(id != person.id) return false;
return Objects.equals(name, person.name);
}
I don`t understand operations over result.
Why do we have to do these operations?
Will be very grateful if someone can explain it to me
Equals i understand well, but i read they are connected somehow, so I’ve attached the comparison code just in case
>Solution :
Explain res = id^(id >>> 32) please
id >>> nfor an int, shifts id rightn%32bits without extending the negative sign bit if one is present. So(id >>> 32)is effectively == id.^is an exclusive OR which says for any bitaandb,a^b == 1ifa and bare different otherwise theya^b == 0. E.g.1^1 == 0. 0^0 == 0, 1^0 == 1
So id ^ id == 0.
if id is a long, (id >>> 32) shifts the high order 32 bits of that long right 32 bits and then exclusive ors the original long with that value.
The over all computation of hashCode is to use that technique sometimes employed with others to generate a number for use in data structures that are used to index by hashcode. in Java, primary examples are Maps and Sets.
Here is a demo.
long v = Long.MIN_VALUE; // has 1 in hob (high order bit) and rest is 0's
System.out.println(Long.toBinaryString(v));
long result = v ^ (v >>> 32); // now hob should be 1 and hob of low order int should be 1
System.out.println(Long.toBinaryString(result));
1000000000000000000000000000000000000000000000000000000000000000
1000000000000000000000000000000010000000000000000000000000000000