

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Obtain the hash code for a string in Java
The hashCode() method is used to obtain the hash code for a string. This method does not accept any parameters as it is a default method and it returns a hash code value.
A program that demonstrates the hashCode() method in Java is given as follows:
Example
import java.io.*; public class Demo { public static void main(String args[]) { String str = new String("The sky is blue"); System.out.println("The string is: " + str); System.out.println("The Hashcode for the string is: " + str.hashCode()); } }
Output
The string is: The sky is blue The Hashcode for the string is: -729257918
Now let us understand the above program.
The string str is defined. Then the string is printed and its HashCode is also printed using the hashCode() method. A code snippet which demonstrates this is as follows:
String str = new String("The sky is blue"); System.out.println("The string is: " + str); System.out.println("The Hashcode for the string is: " + str.hashCode());
- Related Questions & Answers
- Format Specifier for Hash Code in Java
- Get Hash Code for Integer in Java
- Get the hash code for this instance in C#
- Get the hash code for the current Decimal instance in C#
- Get the hash code for the current Int64 instance in C#
- How to get hash code for the specified key of a Hashtable in C#?
- How to obtain the highest occurring character in a String using C#?
- C++ code to find index where this is a hash collision
- Java string concat() sample code examples.
- Java string comparison sample code examples
- How to obtain the time taken for a method to be executed in TestNG?
- Java string case change sample code examples.
- Traversing contents of a hash map in Java
- Execute a String of Code in Python
- Java Program to convert ASCII code to String
Advertisements