Java HashCode for Boolean Types

Java applications can store data of various types, with individual items of data appearing within programs as values or variables. Boolean types can only have a value of either true or false. Programmers can choose between primitive and object types for the Boolean values in their applications. When using object type Booleans, programmers can also generate hash-codes, using the "hashCode" method. Hash-codes provide programs with integer values to represent object types, which can improve efficiency in the data storage for an application.

  1. Boolean Objects

    • Boolean objects store single boolean values using the Boolean wrapper class. The following sample Java code demonstrates creation of a Boolean variable in a program: Boolean isCorrect = Boolean.valueOf(true);

      Unlike most object types, Boolean objects are normally created using this static method rather than the constructor method for the class. The code passes a boolean value to the method as a parameter. This is common with wrapper classes. The code could alternatively pass a primitive type boolean variable to the method, as follows:
      boolean boolValue = true;
      Boolean isCorrect = Boolean.valueOf(boolValue);

      The use of lower case for the initial letter of the boolean type indicates that it is primitive, while the upper case version indicates the object type.

    Hashcode Method

    • All Java objects provide a hashCode method. The Boolean class overrides the default Object class method. The following code demonstrates calling the method on the Boolean variable: isCorrect.hashCode();

      This code calls the method on an object instance of the class. All objects in Java provide the hashCode method for applications involving hash table storage structures. The hashCode works on the basis that two objects that are considered equal will return the same hash integer value. In most cases, different objects of the same type that are not equal will return distinct values as their hash-codes.

    Returns

    • The Boolean class hash-code returns one of two integer values. If a Boolean object stores a value of true, the hashCode returned will be 1231. If the object is storing false, its returned hash value will be 1237. The following sample code demonstrates storing the hashCode for the object in a primitive type integer variable for subsequent use: int hashBool = isCorrect.hashCode();

      The program can then refer to this number variable for further processing.

    Equals Method

    • The hashCode method for Java objects is often referred to in conjunction with the equals method. The equals method allows developers to implement comparisons between application objects. Some objects have unpredictable results with the equals method, depending on the implementation provided within their class declarations. If a class considers two object instances to be equal if they store the same data value, then the hashCode method for that class should return the same integer value for those two instances. This is the case for Boolean object types.

Related Searches:

References

Resources

Comments

Related Ads

Featured