Is there any particular reason Eclipse generated equals that uses the values โ€‹โ€‹1231 and 1237 for boolean elements?

The name basically says it all. I tried Google, but I am returning a load of false positives. I guess I'm just wondering if there was any justification for these two specific numbers, or could they easily find many other sets of numbers?

Edit: And, since the source of the numbers was answered, any reason why the authors of the Boolean hashCode method used these numbers (except that they are prime)? Would there be any other set of primes?

+6
source share
5 answers

Hash functions are subject to collisions . The number of collisions can be reduced with the help of primes (think about the prime factors). 1231 and 1237 are both prime numbers.

/ e1
After a little research, I came across this :

Since they most likely will not have common dividers with hashtable size (unless the prime minister itself shares the size of the hash table), the chance of collision in common hash table implementations will be minimized.

+4
source

These numbers come from the official logical API .

+6
source

They apparently briefly call the Java kernel the results of the Boolean class hashCode() .

Returns: integer 1231 if this object represents true; returns the integer 1237 if this object represents false.

A deeper discussion (though one that ends only with assumptions and not with hard facts) can be found here .

+3
source

They come from the boolean hashCode method. These are just 2 large arbitrary primes. See Boolean.hashCode ()

+1
source

This still asks the question of where they came from and why.

Perhaps these are the best estimates of the number of steps from someone's office to the staff cafeteria. Or someoneโ€™s girlfriendโ€™s phone number. Who knows.

The bottom line is that unless you track down the authors of the original (Sun) and ask them, you will never know the real answer. (I assume that they can still remember the real answer and are ready to tell you!)

Let's move forward ...

+1
source

Source: https://habr.com/ru/post/902160/


All Articles