简体   繁体   中英

hashcode is generated on calling hashcode() method

I am bit confused as this question is asked in an interview and i said this: ""hashCode is generated for each object as and when it is created on the heap in the current running application""

but the interview said: "it is generated when we call hashcode method on the object"

More over, I am looking to understand the hashcode(and that too wrt to java) in more depth, please share some links/sources as it is asked extensively in some job interviews

PS: when i do sysout...on an object the output comes as employee@942f533

It depends what you mean here. As other answers mentioned, the function itself is not called when you create it. However,

   90        * As much as is reasonably practical, the hashCode method defined by
   91        * class {@code Object} does return distinct integers for distinct
   92        * objects. (This is typically implemented by converting the internal
   93        * address of the object into an integer, but this implementation
   94        * technique is not required by the ... [JDK]

from http://www.docjar.com/html/api/java/lang/Object.java.html

Since the address of the object is assigned when it's created, you are correct in a sense. However, since it's not required, and many objects define an override, it's not necessarily true for all objects.

Usually in an interview, you have to challenge the interviewer a little bit to describe what you mean. If you do this and you're right, problem solved, if you do this and you're wrong, then you've at least shown you had a deeper understanding than what your original statment showed.

hashcode() is method like any other method. It is not going to be called when object is created, it may be called when you put object in a map.

I think first documentation to read should be: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode ()

Actually, you need to understand usage of the hash code to understand.

Hashcode not generated on object creation but when hashCode() is called.

For every object you might not want to override default implementation of the java.lang.Object's hash code. It's actually required for all the classes which are using hashing algorithm internally. eg HashMap, HashSet etc. If you go and check internal implementation of those method you would find usage of the hash code etc.

Code snippet from the java.util.HashMap:

public V get(Object key) {
    if (key == null)
        return getForNullKey();
    int hash = hash(key.hashCode());
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
         e != null;
         e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
            return e.value;
    }
    return null;
}

As you can see its used to get the right object from the data structure.

If you check comment of the Object hash code it also clearly mentions this

 * Returns a hash code value for the object. This method is 
 * supported for the benefit of hashtables such as those provided by 
 * <code>java.util.Hashtable</code>. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM