English 中文(简体)
在java实施商品商品商品法典
原标题:hashCode implementation in java

我有一条 class Dog 扩展动物

然后,我将大麻守则方法称为如下方法。

Animal animal  = new Dog(200);
System.out.println(animal.hashCode());

Here,
If I ve overridden the hashCode() in the Dog class it will be returned. Else if I ve overridden the hashCode() in the Dog class it will be returned. Else some integer returned.

我想知道......

  • Why does it call the hashCode() of the super class when it is not overriden in the Dog class? How and what the some integer generated

  • When the hashCode is not generated in anywhere. (I have heard that it is the memory location of the object but not sure.)

最佳回答

这被称为“压倒方法”。 hashCode 方法在 java.lang.Object 中定义,基本上位于对象继承结构的顶部,因此它总是可供爪哇定义的任何对象使用。如果该方法在您特定的子类或其父母之一中没有被推翻,则将引用 java.lang.object 中定义的默认行为。Object 将引用。

通常您不应该担心在父对象内部执行散列码的情况, 但默认执行确实使用对象的内部地址。 请注意, 此内部地址正是这个 - JVM 使用 internal 的解释地址, 应用程序不应依赖该地址来产生任何特别有意义的效果 。

您可以在Java语言说明 - 第8.4.8 节读更多关于压倒一切的工作方式。

问题回答

在 java 中, 每一个类中, 如果未明确提及的话, 都会将对象类列为其母类 。 由于对象类定义了散列码方法, 即使您在类中不定义该方法, 也会受到伤害 。 是的, 在 java 中, 默认的散列码执行程序是返回对象的内存位置 。 这样看来是正确的, 似乎两个对象的内存位置与必须相同 。

has Code () 是形成父类对象的父类对象, 所以如果您没有推翻它, 则会调用父类方法 。 我认为您所说的某些整数是超级父类对象生成的 hashCode 。 这表示您没有在动物类中推翻 hashCode 。

一般说来,如果超级类方法不被推翻,则会叫作即时母法。

  1. First In java every class, if not explitcitly mentioned, will have Object class as its parent class
  2. Java doesn t generate hashCode(), i.e. This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language. Most classes (especially if you are going to use it in any of the Collection API) specially in hashcontainer(HashSet and HashMap) should implement their own HashCode (and by contract their own equals method).

如果您有兴趣了解何时实施 hashCode () 和 等效 (), 您可以访问这个网站 < a href="http://www.javabeat.net/2007/08/hashcode-and- equals- methods/" rel="no follow" > http://www.javabeat.net/2007/08/hashcode-and- equals-methods/





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...