HashCode
1.最底层的实现方法为:在Object .java中
public native int hashCode();
2.在HashMap.java中的hashcode方法:
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
3.在Objects中的hashcode方法:
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
4.Object 中实现方法:
public native int hashCode();
到此位置该方法在java中就结束了。
该方法的注释:
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code hashCode} method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hash tables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java™ programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
从最后一句可以看出,该方法的底层不是由java来实现的
but this implementation technique is not required by the Java™ programming language。
Native
native关键字表示的就是使用本机的方法,而不是由java实现的;
先了解一下JNI:Java Native Interface(Java本机接口)
JNI是一个本地编程接口。它允许java虚拟机(VM)内运行的java代码与应用C++、C++和汇编等其他编程语言编写的应用程序和库进行互操作。
JNI最重要的好处是它对底层java虚拟机(VM)的实现没有任何限制。因此,java虚拟机(VM)供应商可以添加对JNI的支持,而不会影响VM的其他部分。程序员可以编写一个本机应用程序或库的一个版本,并期望它与支持JNI的所有java虚拟机(VM)一起工作。
那为什么java要结合其他语言来工作呢?
- 标准Java类库不支持应用程序所需的依赖于平台的特性。
- 对自己已有的一个用另一种语言编写的库,并希望通过JNI使Java代码可以访问它。
- 希望用较低级别的语言(如assembly)实现一小部分时间关键型代码。
通过使用JNI之后,可以实现的一些内容有:
- 创建、检查和更新Java对象(包括数组和字符串)。
- 调用Java方法。
- 捕捉并抛出异常。
- 加载类并获取类信息。
- 执行运行时类型检查。
还可以将JNI与调用API一起使用,以使任意本机应用程序能够嵌入java vm。这可以轻松地使现有的应用程序支持Java,而不必与VM源代码连接。
JRI:Java Runtime Interface (Java运行时接口)
Netscape提出了Java运行时接口(JRI),它是Java虚拟机中提供的服务的通用接口。JRI的设计考虑到了可移植性,它对底层javavm中的实现细节几乎没有任何假设。JRI解决了广泛的问题,包括本机方法、调试、反射、嵌入(调用)等。
用C编写native方法
步骤如下:
- 编写带有 native 声明的方法的java类,生成.java文件;(注意这里出现了 native 声明的方法关键字)
- 使 用 javac 命令编译所编写的java类,生成.class文件;
- 使用 javah -jni java类名 生成扩展名为 h 的头文件,也即生成.h文件;
- 使用C/C++(或者其他编程想语言)实现本地方法,创建.h文件的实现,也就是创建.cpp文件实现.h文件中的方法;
- 将C/C++编写的文件生成动态连接库,生成dll文件;