Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does hashcode number represent the memory address? [duplicate]

Tags:

java

I have learned that hashcode is a Unique Identification reference number, which is a hexadecimal number.
My doubt is, does the reference number represents the memory address of the object?

For example:

Employeee e1=new Employee();
System.out.println(e1.hashcode());

Will this code return me the memory address of my object?

like image 689
user2301829 Avatar asked May 07 '13 12:05

user2301829


People also ask

Is hashCode same as memory address?

The default implementation of hashCode() in Object class is possibly unknown, but definitely it's not memory-address representation.

What is hashCode number?

A hash code is an integer value that is associated with each object in Java. Its main purpose is to facilitate hashing in hash tables, which are used by data structures like HashMap.

What does hashCode represent in Java?

The hashCode() method is defined in Java Object class which computes the hash values of given input objects. It returns an integer whose value represents the hash value of the input object. The hashCode() method is used to generate the hash values of objects.

Can hashCode be duplicated?

it is possible that two instances can give the same hash code, but this is unlikely.


1 Answers

Hashcode is not a unique identification. It's just a number that helps you distinguish objects. Two different objects may have the same hash code and it is fine.

HashCode characteristics:

  1. If obj1 and obj2 are equal, they must have the same hash code.
  2. If obj1 and obj2 have the same hash code, they do not have to be equal.
like image 52
Michal Borek Avatar answered Sep 24 '22 22:09

Michal Borek