The java.lang String hashCode() method returns the hash code value for the String object. The hash code is a unique integer value that represents the content of the String. It is widely used in hashing and indexing algorithms.
Examples: 1.
String s1 = "Hello"; int h1 = s1.hashCode();
String s2 = "hello"; int h2 = s2.hashCode();
System.out.println(h1); System.out.println(h2);
Output: 69609650 99162322
In this example, we have two String objects with different content, but the same hash code. This can happen because the hash code function is not guaranteed to produce unique values for all strings.
2.
String s1 = "Hello"; String s2 = "Hello"; int h1 = s1.hashCode(); int h2 = s2.hashCode();
System.out.println(h1); System.out.println(h2);
Output: 69609650 69609650
In this example, we have two String objects with the same content, and therefore the same hash code.
Package library: java.lang. This method is part of the core Java library, which is loaded by default when running Java programs.
Java String.hashCode - 30 examples found. These are the top rated real world Java examples of java.lang.String.hashCode extracted from open source projects. You can rate examples to help us improve the quality of examples.