Search code examples
javahashcodeatomicboolean

What is the hashCode of an AtomicBoolean?


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?


Solution

  • The package summary tells why hashCode is not overriden for AtomicBoolean:

    Atomic classes are not general purpose replacements for java.lang.Integer and related classes. They do not define methods such as equals, hashCode and compareTo. (Because atomic variables are expected to be mutated, they are poor choices for hash table keys.)

    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.