Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any particular reason why Eclipse generated equals uses the values of 1231 and 1237 for booleans?

Title basically says it all. I've tried Googling but return a load of false positives. I guess I'm just wondering if there was a certain rationale behind these two specific numbers or could they have easily been many other sets of numbers?

Edit: And, since the source of the numbers has been answered, any reason why writers of the Boolean hashCode method used those numbers (besides that they're prime)? Would any other set of prime numbers worked just as well?

like image 417
AHungerArtist Avatar asked Nov 22 '11 22:11

AHungerArtist


2 Answers

These numbers come from the official Boolean API.

like image 84
Beau Grantham Avatar answered Sep 28 '22 18:09

Beau Grantham


Hash functions are prone to collisions. The number of collisions can be reduced by using prime numbers (think about the factors prime numbers have). 1231 and 1237 are both prime numbers.

/e1
After doing a little more research I came across this:

Since they most probably will have no common divisors with the hashtable size (unless the prime itself divises the hashtable size), the chances of collision in common hashtable implementations would be minimized.

like image 21
Jeffrey Avatar answered Sep 28 '22 20:09

Jeffrey