java native关键字

在看jdk源码的时候看到了对象的hashcode()方法之前添加了native关键字,而且方法没有实现,具体如下:

/** *(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.) 
*/ 
public native int hashCode(); 

根据注释我们可以看到,对象的hashcode的生成是通过将对象的内部地址转换为整形数值实现的,但是方法不通过java语言实现。

JNI是Java Native Interface的缩写,它允许java和其他语言写的代码进行交互。
JNI的书写步骤:
1.编写带有native关键字的java类方法
2.使用javac编译java类
3.使用javah ?jni 类名 生成扩展名为.h的头文件
4.使用C或者C++或者其他语言实现本地方法
5.将C或者C++实现的文件生成动态链接库

具体的书写例子可以参照: http://www.blogjava.net/shiliqiang/articles/287920.html

你可能感兴趣的:(java,native)