Why Does an Immutable String Calculate Its Hash Value?

Why Does an Immutable String Calculate Its Hash Value? thumbnail
Java uses standard frameworks for storing items of data.

Java applications model code components as objects, with each object having an area of responsibility. Objects provide methods to carry out their responsibilities. Some methods in Java are implemented by all possible objects, including strings. The Java framework allows developers to use certain types of data storage structure, some of which use hash-code values to store items. A hash-code value uses a code to refer to a specific object, allowing the collection class to implement efficient methods for storing multiple objects within the program. Java code can calculate hash-code values for all objects, including strings, which are immutable.

  1. String Content

    • String variables in Java programs are objects of the String class, storing sequences of text characters. This can be confusing, because programs create string variables using the same type of structure used for primitive type variables such as numbers. The following sample code demonstrates creating a string variable:
      String name = "John";

      The program can carry out methods of the String class on this variable, some of which involve altering the text content. Methods that alter the content of a string normally return a new string, rather than altering the existing string. This is because strings are immutable, meaning that once a string variable is created, its content cannot be altered. Programs in which the content of a string appears to be altered are actually creating new strings to replace existing ones.

    HashCode Function

    • All objects in Java provide the "hashCode" function. This is because all Java classes inherit from the superclass Object, which provides the "hashCode" method. The following sample code demonstrates calling the method on the String variable:
      name.hashCode();

      Calling this method causes Java to generate a hash-code value for the string variable specified. Creating a hash-code value for a string object involves the same process as you would use for any object.

    Result

    • The "hashCode" method results in an integer value representing the object in question. This number allows Java classes to optimize on efficiency when storing data values, particularly in conjunction with the HashMap class or other related collections classes. In most cases, a Java programmer does not need to worry about the details of the numbers used as hash-codes for items such as strings, as the collections classes handle this detail.

    Equality

    • The result of calling the "hashCode" on two items that are equal should be the same number. For example, the following two string variables should generate the same hash-code value:
      String someName = "John";
      String otherName = "John";

      If two objects return a true result when used within the equals method, then Java considers them to be equal, in which case they should result in the same hash-code. For example, the following code would result in a true value:
      boolean areEqual = someName.equals(otherName);

      It is typically the case that if two strings are not equal, they will generate different hash-code values, but this is not guaranteed. (See References 1, 2, 3)

Related Searches:

References

Resources

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images

Comments

Related Ads

Featured