简体   繁体   English

hashcode是在调用hashcode()方法时生成的

[英]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"" 我有点困惑,因为这个问题在一次采访中被问到,我说:“”当每个对象在当前运行的应用程序中的堆上创建时,会生成hashCode“”

but the interview said: "it is generated when we call hashcode method on the object" 但采访中说:“它是在我们对对象调用hashcode方法时生成的”

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 更重要的是,我希望更深入地理解哈希码(对于java来说太过分了),请分享一些链接/来源,因为在一些求职面试中会广泛询问

PS: when i do sysout...on an object the output comes as employee@942f533 PS:当我在一个对象上做sysout时,输出来自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 来自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. hashcode()就像任何其他方法一样。 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 () 我认为首先要阅读的文档应该是: 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. Hashcode不是在对象创建时生成的,而是在调用hashCode()时生成的。

For every object you might not want to override default implementation of the java.lang.Object's hash code. 对于每个对象,您可能不希望覆盖java.lang.Object的哈希代码的默认实现。 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. 例如HashMap,HashSet等。如果你去检查那些方法的内部实现,你会发现哈希码的使用等。

Code snippet from the java.util.HashMap: 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>. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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