Question with Java. Lang. Object. hashcode ()

Source: Internet
Author: User
Question:

1. In the Java API Doc, I saw the statement below:

This integer need not remain consistent from one execution of an application to another execution of the same application.

Does it mean that on each execution, the hashcode may be different?

But I run this programme (fragment below) sereral times:

 
System. Out. println ("C1 hashcode:" + c1.hashcode (); system. Out. println ("C2 hashcode:" + c2.hashcode ());

They return the same code, why?

2. I saw this statement:

It is not required that if two objects are unequal according to the equals (Java. lang. object) method, then calling the hashcode Method on each of the two objects must produce distinct integer results. however, the programmer shocould be aware that producing distinct integer results for unequal objects may improve the performance of HashTables.

Does it mean that it's possible that unequal objects shoshould produce the same hash code? If it does, how to avoid it?

____________________________________________________________________________

Answers:

> They return the same code, why?

The docs say that it need not be consistent. But it doesn't say that it will not be consistent at all. It might give you the same value.

> Does it mean that it's possible that unequal objects

> Shocould produce the same hash code? If it does, how

> Avoid it?

If you override the hashcode () method in the object to provide a new implementation in your class, this might be possible. hashmap uses the hashcode to lookup for objects, hence it is advised to have them unique. the uniqueness is not an essential characteristic of the hashcode () method.

It means that if you run your program several times, the "same" object may have a different hashcode each time.

Within one run of your application, if you call hashcode multiple times on the same object (and you haven't changed the state of that object) You must get the same value each time.

The hashcode is calculating according object's content. If the two different object has the same content, the hashcode maybe the same. For example,

String A = new string ("James ");

String B = new string ("James ");

System. Out. println (A. hashcode ());

System. Out. println (B. hashcode ());

The result will show two same integers.

In another case. The hashcode () is according algorithm. For example. I can create my class and override the hashcode () method like:

Class {

Int;

Int hashcode (){

Return a % 10;

}

}

Thsu, when a is 7, 17, 27 and so on, their hashcode will be same one.

In cases where the programmer has implemented the hashcode method. it is highly unlikely that the design wocould change the hashcode on subsequence runs of the JVM. if hashcode has not been implemented, it will almost certainly change.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.