Maybe true and false are 1 and 0, but in Java they hash to 1231 and 1237

Image for post
Image for post
Photo by Chris Ried on Unsplash
    @Override
public boolean equals(Object obj)
    if (objectA.equals(objectB)) {
assertEquals(objectA.hashCode(), objectB.hashCode());
}
    if (objectA.equals(objectB)) {
assertEquals(objectA.hashCode(), objectB.hashCode());
} else {
assertNotEquals
(objectA.hashCode(), objectB.hashCode());
}
    @Override
public int hashCode() {
int hash = 1;
// ...do some stuff with the hash codes of object fields...
if (this.someFlag) {
hash *= 3;
} else {
hash *= 17;
}
return hash;
}

Side bar: hash codes in Scala

scala> (Math.abs(_: Double)).hashCode
res15: Int = 2099559808
scala> (Math.abs(_: Int)).hashCode
res16: Int = 284101091
scala> 7.hashCode
res16: Int = 7
scala> -7.hashCode
res17: Int = -7
scala> Math.PI.hashCode
res18: Int = 340593891
scala> true.hashCode
res19: Int = 1231
scala> false.hashCode
res20: Int = 1237
scala> System.identityHashCode(res16)
res21: Int = 224237360
scala> System.identityHashCode(7)
res22: Int = 224237360
scala> System.identityHashCode(res17)
res23: Int = 1098912468
scala> System.identityHashCode(-7)
res24: Int = 1098912468
scala> System.identityHashCode(res18)
res25: Int = 773074354
scala> System.identityHashCode(Math.PI)
res26: Int = 949389896
scala> System.identityHashCode(res19)
res27: Int = 1469015613
scala> System.identityHashCode(1231)
res28: Int = 827221909
scala> System.identityHashCode(res20)
res29: Int = 1249746110
scala> System.identityHashCode(1237)
res30: Int = 1576743136
scala> System.identityHashCode(7)
res31: Int = 224237360
scala> System.identityHashCode(-7)
res32: Int = 1098912468
Image for post
Image for post
I think there are hash browns in the background. Photo by Gabriel Gurrola on Unsplash

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store