Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Object#hashCode() returns int instead of long

Tags:

why not:

public native long hashCode(); 

instead of:

public native int hashCode(); 

for higher chance of achieving unique hash codes?

like image 945
dimitrisli Avatar asked Nov 12 '10 15:11

dimitrisli


People also ask

Why do we use object?

An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Why object is used in programming language?

Objects are designed to share behaviors and they can take on more than one form. The program will determine which meaning or usage is necessary for each execution of that object from a parent class, reducing the need to duplicate code. A child class is then created, which extends the functionality of the parent class.

Why objects are used in OOPs?

With OOP, instead of writing a program, you create classes. A class contains both data and functions. When you want to create something in memory, you create an object, which is an instance of that class. So, for example, you can declare a Customer class, which holds data and functions related to customers.

What is a object in programming?

In object-oriented programming (OOP), objects are the things you think about first in designing a program and they are also the units of code that are eventually derived from the process.


1 Answers

Because the maximum length of an array is Integer.MAX_VALUE.

Since the prime use of hashCode() is to determine which slot to insert an object into in the backing array of a HashMap/Hashtable, a hashcode > Integer.MAX_VALUE would not be able to be stored in the array.

like image 109
matt b Avatar answered Oct 03 '22 08:10

matt b