I couldn't find any information in the official docs. I know that Boolean.hashCode(boolean b) returns the two primes 1231 and 1237 for true and false. I am hoping for a similar implementation in AtomicBoolean. But in the decompiled class file it appears to call public native int hashCode(); of Object - does that mean it will return the memory location?
What is the hashCode of an AtomicBoolean?
205 views Asked by jcfrei At
4
There are 4 answers
0
On
AtomicBoolean doesn't override hashCode (or equals), so it inherits the default Object behavior.
This actually makes a lot of sense. The main use case of hashCode is to let the object be the key of a hash map, but it's dangerous to mutate keys once they're in a map. Since the main attribute of an AtomicBoolean is its mutability, this makes it a bad candidate for a HashMap key.
In other words: If you need it to be a HashMap key, you shouldn't mutate it, and if you don't intend to mutate it, then you shouldn't use an AtomicBoolean.
The package summary tells why
hashCodeis not overriden forAtomicBoolean:does that mean it will return the memory location?
It depends on what JVM you use, but yes, it's usually derived from the memory address. Other JVMs may just use a random number.