Note that the parameter of Objects.hash is Object.... This has two main consequences:

  • Primitive values used in the hash code calculation have to be boxed, e.g. this.id is converted from long to Long.
  • An Object[] has to be created to invoke the method.

The cost of creating of these "unnecessary" objects may add up if hashCode is called frequently.

Answer from Andy Turner on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Objects.html
Objects (Java Platform SE 8 )
October 20, 2025 - Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]).
🌐
Medium
medium.com › @AlexanderObregon › javas-objects-hash-explained-edb07b463c56
Java’s Objects.hash() Explained | Medium
September 18, 2024 - Field Hash Codes: For each significant field, you compute its hash code (if the field is an object, you call hashCode() on it; if it's a primitive, you can use the wrapper class's hashCode() method).
Discussions

What exactly is an objects hash code?
I did a quick Google search. It seems Java does not put the hash code after the @, but like Kotlin, it prints the address in memory. So I don't think the hash code is used in the toString() method (by default) in either language. More on reddit.com
🌐 r/learnprogramming
6
4
November 25, 2022
What's the implementation of hashCode in java Object? - Stack Overflow
The accepted answer to the "possible ... to the Object class - how can it possibly answer this question? People mark way to much stuff as duplicates and it annoys me. Is it possible to unmark this as a duplicate or link it correctly to a duplicate that does answer the same question, such as this one: stackoverflow.com/questions/2237720/… ... hashCode is a native method which means that a system library is called internally. See Java Native Interface ... More on stackoverflow.com
🌐 stackoverflow.com
java 7 - Objects.hash() vs Objects.hashCode(), clarification needed - Stack Overflow
0 Objects.hashCode versus Object::hashCode in Java 7 and later More on stackoverflow.com
🌐 stackoverflow.com
Can someone ELI5 the hashcode() method for me?
This is not material that any 5 year old would understand. HashCode is a 'helper' function that allows collections like HashMap to guess where they stored an object. Lets say you have a list of strings (apple, analog, bee and data) and you want to 'test' if the string "cider" is in that list. With a simple list you would have to, worst case, check all the items in the list. In this case 4. Hashmaps/sets use the 'trick' that objects can give a short number that is calculated from their contents. Lets say we simply use the alphabet position of the first character of the string. So apple and analog would be 1, bee would be 2, data would be 4. We would then create a bunch of lists based on those codes. We would stuff apple and analog into list 1, bee in list 2, data in list 4. If we were then to test "cider" which would have code 3 we would not have to check any list because we know there isn't even a list with code 3, let alone any strings in there. Same if we wanted to test "art": we know it also has hashcode 1 so we only need to test a list of 2 items instead of all 4 items. This is as simple as I can explain what a HashSet / Map does and why it needs a hashcode. In reality how it decides to create these internal lists and how a hashcode is made is a bit more complex but it essentially works like this. More on reddit.com
🌐 r/java
18
5
November 2, 2014
🌐
Reddit
reddit.com › r/learnprogramming › what exactly is an objects hash code?
r/learnprogramming on Reddit: What exactly is an objects hash code?
November 25, 2022 -

I was completing a Kotlin topic on Jetbrains Academy about toString() and came across a section saying this about it:

However, for most classes, by default, toString() still returns the name of the class and the address where the object is located in the memory.

I'm assuming that toString() in Kotlin functions the same way as it does in Java. In Java it takes the name of the objects class and appends the hash code to it which seems to be what is explained above. The only issue I have is that I never heard of an objects hash code being its physical location in memory or the JVM.

How true is this?

🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java object hashcode method
Java Object hashCode Method
September 1, 2008 - The Java Object hashCode() method returns a hash code value for the object.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Object.html
Object (Java Platform SE 8 )
October 20, 2025 - As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-equals-hashcode
Java equals() and hashCode() | DigitalOcean
August 3, 2022 - Java Object hashCode() is a native method and returns the integer hash code value of the object.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › java objects.hash() vs objects.hashcode()
Java Objects.hash() vs Objects.hashCode() | Baeldung
June 24, 2025 - With that said, let’s look at how we might have implemented Player.hashCode() prior to Java 7: @Override public int hashCode() { int result = 17; result = 31 * result + firstName != null ? firstName.hashCode() : 0; result = 31 * result + lastName != null ? lastName.hashCode() : 0; result = 31 * result + position != null ? position.hashCode() : 0; return result; } Because both Objects.hashCode() and Objects.hash() were introduced with Java 7, we have to explicitly check for null before calling Object.hashCode() on each field.
🌐
Coderanch
coderanch.com › t › 321515 › java › HashCode
What is HashCode [Solved] (Java in General forum at Coderanch)
Sometimes a box will have more ... a whole pile of cabbages, peas , banjos and rhinoceroses. That's a hash code. A way of getting a number from an object so it can be stored in a Hashtable. In Java a hash code can be any integer, and each object type is responsible for ...
🌐
Baeldung
baeldung.com › home › java › core java › guide to hashcode() in java
Guide to hashCode() in Java | Baeldung
December 8, 2025 - In general, there’s no universal recipe when it comes to implementing hashCode(). We highly recommend reading Joshua Bloch’s Effective Java. It provides a list of thorough guidelines for implementing efficient hashing algorithms. Notice here that all those implementations utilize number 31 in some form. This is because 31 has a nice property. Its multiplication can be replaced by a bitwise shift, which is faster than the standard multiplication: ... When we need to retrieve an object’s identity-based hash value, even when the hashCode() method is overridden, Java provides the System.identityHashCode(Object) method.
🌐
Medium
medium.com › @n20 › understanding-hashcode-and-equals-the-basics-of-hashing-e8b769425bed
Understanding hashCode() and equals(): The Basics of Hashing | by MobileDev - NK | Medium
September 25, 2024 - The hashCode() method returns an integer value, known as the hash code, that represents an object. This hash code is used to determine which "bucket" an object will be placed into when it’s stored in a hash-based collection.
🌐
CodeGym
codegym.cc › java blog › java objects › java hashcode()
What is Java hashcode
January 8, 2025 - By default, the hashCode() function for an object returns the number of the memory cell where the object is stored. Therefore, if no changes are made to the application code, then the function should return the same value.
🌐
Programiz
programiz.com › java-programming › library › object › hashcode
Java Object hashCode()
class Main { public static void main(String[] args) { // hashCode() with Object Object obj1 = new Object(); System.out.println(obj1.hashCode()); // 1785210046 Object obj2 = new Object(); System.out.println(obj2.hashCode()); // 1552787810 Object obj3 = new Object(); System.out.println(obj3.hashCode()); // 1361960727 } } Note: The Object class is the super class for all the classes in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › method-class-hashcode-method-in-java
Method Class | hashCode() Method in Java - GeeksforGeeks
July 11, 2025 - Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Object › hashCode
Java Object hashCode() - Generate Hash Code | Vultr Docs
November 19, 2024 - Generate an override for the hashCode() method in your custom class. Use the fields of the object to calculate a hash code.
🌐
JavaSart
javastart.pl › strona główna › baza wiedzy › java - programowanie obiektowe › metoda hashcode()
Metoda hashCode() - JavaStart
September 22, 2022 - Metoda hashCode() jest dziedziczona z klasy Object i domyślnie powinna zwrócić unikalną wartość dla każdego obiektu. Jeśli jej nie nadpiszemy, to zwróci ona różne wartości nawet dla obiektów, które pod względem przechowywanych ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Object.html
Object (Java Platform SE 7 )
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java
Top answer
1 of 4
100

See the documentation for hashCode and hash. hash takes Object... while hashCode takes Object. The example given is:

@Override public int hashCode() {
    return Objects.hash(x, y, z);
}
  • Objects.hash(Object... values) should be used in cases when you want a hash of a sequence of objects, e.g. when defining your own hashCode method and want a simply-coded hash for multiple values that make up the identity of your object.
  • Objects.hashCode(Object o) should be used when you want the hash of a single object, without throwing if the object is null.
  • Object::hashCode() should be used when you want the hash of a single object, and will throw an exception if the object is null.

Note that hash(o) and hashCode(o) won't necessarily return the same thing! If you're doing it for a single object, you should probably use hashCode.

2 of 4
46

Objects.hashCode

The utility method Objects.hashCode( Object o ) simply calls the hashCode method on the passed object.

Tolerates NULL

So why invent or use this method? Why not just call the object’s hashCode method yourself?

This method offers one benefit: NULL0. The utility method tolerates a null.

  • If you call Objects.hashCode( myObject ) where myObject is NULL, you get back a zero (0).
  • In contrast, calling myObject.hashCode() when myObject is NULL throws a NullPointerException argument.

Whether tolerating a null is desirable or not depends on your own judgement in your particular situation.

Objects.hash

The utility method Objects.hash( Object o , … ) serves a different purpose. This method has two phases:

  • Call .hashCode on each passed object, collecting each result.
  • Calculate a hash on the collected results.

hash of a hash

If you pass a single object, Objects.hash( myObject ), first myObject.hashCode is called and collected, and then a hash on that single-item collection is calculated. So, you end up with a hash of a hash.

When hashing a single object, it is vital to understand that Objects.hashCode( myObject ) returns a different result than Objects.hash( myObject ). Effectively, the second returns a hash on the result of the first.

Annoying in practice

The logic of the approach taken in these two Objects methods makes sense, in and of themselves.

Unfortunately, in practice, for those of us trying to use them in day-to-day use when writing code on our POJOs to override hashCode, and correspondingly equals, we must think twice to decide which to call.

  • If your hashCode (and equals) override is based on a single member of your class, use Objects.hashCode( member ).
  • If your hashCode (and equals) override is based on multiple attribute of your class, use Objects.hash( memberA , memberB , memberC ).

Single member, not tolerating a NULL

@Override
public int hashCode() {
    return this.member.hashCode() ;  // Throws NullPointerException if member variable is null.
}

Single member, tolerating a NULL

@Override
public int hashCode() {
    return Objects.hashCode( this.member ) ;  // Returns zero (0) if `this.member` is NULL, rather than throwing exception.
}

Multi-member, tolerating a NULL

@Override
public int hashCode() {
    return Objects.hash( this.memberA , this.memberB , this.memberC  ) ;  // Hashes the result of all the passed objects’ individual hash codes.  
}

Example

We can test these various methods quite simply.

UUID

Let's use a UUID object as an example. A UUID (universally unique identifier) is a 128-bit value where certain bits have certain semantics.

The OpenJDK implementation of UUID internally represents the 128-bit value as a pair of 64-bit long integer numbers.

That same implementation overrides Object::equals and Object::hashCode to look at the data stored in that pair of long integers. Here is the source code for those two methods.

public boolean equals(Object obj) {
    if ((null == obj) || (obj.getClass() != UUID.class))
        return false;
    UUID id = (UUID)obj;
    return (mostSigBits == id.mostSigBits &&
            leastSigBits == id.leastSigBits);
}
public int hashCode() {
    long hilo = mostSigBits ^ leastSigBits;
    return ((int)(hilo >> 32)) ^ (int) hilo;
}

Example code

Instantiate our UUID object.

UUID uuid = UUID.randomUUID();

Calculate our hash values.

int hash1 = uuid.hashCode();
int hash2 = Objects.hashCode( uuid );  // Result matches line above.

int hash3 = Objects.hash( uuid );  // Returns a hash of a hash.
int hash4 = Objects.hash( uuid.hashCode() ); // Result matches line above.

Dump to console.

System.out.println( "uuid.toString(): " + uuid.toString() );
System.out.println( " 1/2 = " + hash1 + " | " + hash2 );
System.out.println( " 3/4 = " + hash3 + " | " + hash4 );

See this code run live at IdeOne.com.

uuid.toString(): 401d88ff-c75d-4607-bb89-1f7a2c6963e1

1/2 = 278966883 | 278966883

3/4 = 278966914 | 278966914