what is HashCode in Java

Showing Answers 1 - 50 of 50 Answers

Ashok babu

  • Jan 30th, 2006
 

Hash code is an unique id number allocated to an object by JVM. Through this hash code only the object is referenced.It is Hexadecimal form.

  Was this answer useful?  Yes

sekhar

  • Apr 25th, 2006
 

Hash code is not an unique number for an object.If two objects are equals(means ob1.equals(ob2))then these two objects return same hash code.so we have to implement hashcode() of a class in such way that if two objects are equals(that is compared by equal() of that class)then those two objects must return same hash code.

  Was this answer useful?  Yes

Hash Code is actually a method which is overriden always when the equals() method is overridden. HashCode returns an integer after the comparison of two objects if are equal then returns an equal int for the both objects else a diiferent number always if the two objects are not same.

  Was this answer useful?  Yes

True what a hashcode method returns is integer, but on JVM it is stored as hexa only.
HashMaps are nothing but an array of arrays.
So when you store a key/value pair in it, for a class a specific subarray id utilized.
Those arrays are pointed by there addresses which are in hex form only, so when u access any element JVM is using same hashcode for that.
When you see hashcode through hashcode() it will be integer makes sense rite?

  Was this answer useful?  Yes

huangtzang

  • Jul 29th, 2009
 

The default hashCode() method uses the 32-bit internal JVM address of the Object as its hashCode. However, if the Object is moved in memory during garbage collection, the hashCode stays constant. This default hashCode is not very useful, since to look up an Object in a HashMap, you need the exact same key Object by which the key/value pair was originally filed. Normally, when you go to look up, you don’t have the original key Object itself, just some data for a key. So, unless your key is a String, nearly always you will need to implement a hashCode and equals method on your key class.

  Was this answer useful?  Yes

rajujohn

  • Aug 3rd, 2009
 

A hash code is a number;
example:
claa A
{

}

In the above class i am creating two instance;

A obj=new A();
B obj1=new A();

now obj is geting the reference and obj1 also getting the reference;
but both obj and obj1 having  reference are difference.
if(obj==obj1)
the condition is false because ,both the obj and obj1 return the different hashcode
so,from this we can know a hash code is a unique id number created by a instance not created by a class variable;
class variable means A obj; here obj is a class variable; new A() is an instrance it only generating the hash code;
by john raju(poothurai)

  Was this answer useful?  Yes

Hash Code is unique value whenever we want to check that two object are equal or not that time we can't check with .equalls or == because those are always gives false. and in that time we can create hash code for those objects and override the .equal() Method. then we can check the two objects are equall or not ohterwise can't.

  Was this answer useful?  Yes

hajidel

  • Dec 7th, 2009
 

HashCode is a method that returns an integer to confirm whether two objects are equal. Such objects are mainly HashMaps and HashSets. The objects are only equal if if their hashCode methods return equal integer values. 

  Was this answer useful?  Yes

Here is the programme for different data type and their o/p

public class  text
{
 public void pro(){
 String s1=new String("ramesh");
 String s2="ramesh";
 System.out.println(s1);
 System.out.println(s2);
 if(s1==s2)
  System.out.println("ok");
 else
  System.out.println("not ");
 if(s1.equals(s2))
  System.out.println("ok");
 System.out.println(s1.hashCode());
  System.out.println(s2.hashCode());

System.out.println("**************************");

Integer i=new Integer(1);
Integer i1=new Integer(1);
System.out.println("**************"+i);
System.out.println("*****************"+i1);
System.out.println("----------------"+i.hashCode());
System.out.println("----------------"+i1.hashCode());
if(i.equals(i.hashCode()))
 System.out.println("ramesh babu");

 }
 public static void main(String[] args)
 {
  text t=new text();
    text t1=new text();
        System.out.println(t);
            System.out.println(t1);
    System.out.println(t.hashCode());
        System.out.println(t1.hashCode());
  t.pro();
 }
}

text@1a46e30
text@3e25a5
27553328
4072869
ramesh
ramesh
not
ok
-938314596
-938314596
**************************
**************1
*****************1
----------------1
----------------1

Ramesh Babu

  Was this answer useful?  Yes

kirag

  • Feb 17th, 2010
 

Hash code is not unique for an object.


Two or more objects can have the same hashcode and still be unequal.
For two objects to be equal, they must have the same hashcode (necessary condition)

But, if two objects have different hashcodes, they are definately unequal (sufficient condition)


  Was this answer useful?  Yes

thiyagarajan

  • Aug 5th, 2011
 

it returns hashcode value for an object

  Was this answer useful?  Yes

Chandrasekhar

  • Sep 16th, 2011
 

Here is the program for HashCode

Code
  1. // Main Program

  2. package com.java.example;

  3.  

  4. public class HashCodeExample {

  5.  

  6.         /**

  7.          * @param args

  8.          */

  9.         public static void main(String[] args) {

  10.                 Employee emp1 = new Employee(1234, "Java");

  11.                 Employee emp2 = new Employee(1234, "Java");

  12.                 String Java = "Java";          

  13.                 System.out.println("Hashcode for emp1 = "+emp1.hashCode());

  14.                 System.out.println("Hashcode for emp2 = "+emp2.hashCode());            

  15.                 System.out.println("Objects are equal : "+emp1.equals(emp2));

  16.                 System.out.println("Java Hash Code    = "+Java.hashCode());

  17.         }

  18. }

  19.  

  20.  

  21.  

  22. //Employee Bean

  23.  

  24. package com.java.example;

  25.  

  26. public class Employee {

  27.  

  28.         public int employeeId;

  29.         public String name;    

  30.  

  31. //HashCode

  32.         @Override

  33.         public int hashCode() {

  34.                 final int prime = 31;

  35.                 int result = 1;

  36.                 result = prime * result + employeeId;

  37.                 result = prime * result + ((name == null) ? 0 : name.hashCode());

  38.                 return result;

  39.         }

  40.  

  41.         @Override

  42.         public boolean equals(Object obj) {

  43.                 if (this == obj)

  44.                         return true;

  45.                 if (obj == null)

  46.                         return false;

  47.                 if (getClass() != obj.getClass())

  48.                         return false;

  49.                 Employee other = (Employee) obj;

  50.                 if (employeeId != other.employeeId)

  51.                         return false;

  52.                 if (name == null) {

  53.                         if (other.name != null)

  54.                                 return false;

  55.                 } else if (!name.equals(other.name))

  56.                         return false;

  57.                 return true;

  58.         }

  59.  

  60.         public Employee(int employeeId, String name) {

  61.                 super();

  62.                 this.employeeId = employeeId;

  63.                 this.name = name;

  64.         }

  65.  

  66.         public int getEmployeeId() {

  67.                 return employeeId;

  68.         }

  69.  

  70.         public void setEmployeeId(int employeeId) {

  71.                 this.employeeId = employeeId;

  72.         }

  73.  

  74.         public String getName() {

  75.                 return name;

  76.         }

  77.  

  78.         public void setName(String name) {

  79.                 this.name = name;

  80.         }

  81.  

  82. }

  83.  

  84.  

  85. //OutPut

  86. Hashcode for emp1 = 2340721

  87. Hashcode for emp2 = 2340721

  88. Objects are equal : true

  89. Name Hash Code    = 2301506

  90.  

  Was this answer useful?  Yes

Michael Wu

  • Oct 4th, 2011
 

Originally hash code is translate memory address to int. I want to propose a question why java designed a hashcode? It is just for distinguish two object?

  Was this answer useful?  Yes

Abhisek

  • Dec 23rd, 2014
 

Hashcode is nothing but Index of the Address of the Object. Hashcode is integer value.which would be automatically generated by JVM.

whenever an object is created & address is generated,JVM pick up one of the index of the address of the object (hashcode) and links that index of the address to the actual Address.it will assign only index of the address to the reference variable.

whenever we use ( . ) operator in a reference variable .it fetches the actual address using index of the address present inside that reference variable.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions