Java String hashcode() Method

Java String hashcode() Method

What is String?

In Java, A group of characters known as a string class are ones that a script interprets literally. A string is represented as a byte (or term) range information structure in order to preserve a number of components, frequently letters. The sequence may also designate additional types and arrangements of sequence (or array) data, such as more flexible arrays.

  • Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.

To know more about String Class in java read the complete article.

String IndexOf() Method:

Java String hashCode() method yields the string’s integer hash code value. This function of the Object class is overridden by the String class. A hash code for this Function is returned by the hashCode() method of the Method class. The hash code is calculated as the exclusive-or of the hashcodes for the method name and the class name declared in the underlying method.

  • The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

If equals() is true for two strings, their hashCode() will be the same and If two strings hashCode() is equal, it doesn’t mean they are equal.

Syntax:

public int hashCode()

Parameters:

hashCode in class Object

Let’s look at a string-related Java program where the Java String hashcode() Method is used to perform an operation on the given string.

Example 1 : Return the hash code of a string

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "Hello PrepInsta";
    System.out.println(myStr.hashCode());
  }
}

Output

-1835279712

Example 2 : Return the hash code of a string

Run
public class Main {
   public static void main(String args[]) {
      String Str = new String("Welcome to PrepInsta.com");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
   }
}

Output

Hashcode for Str :-1566850982

Example 3: Return the hash code of a string.

Run
public class Main {
   public static void main(String args[]) {
      String Str = new String("Welcome to PrepInsta prime");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
   }
}

Output

Hashcode for Str :1401013862

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription