java原生是什么意思,什么是原生对象?

what is a native object means i found the that java has peer class to interface with native objets?

解决方案

Java programs can use JNI to access to functions implemented in native code (anything compiled to machine code). Interfacing with object oriented native code requires a java class which forwards method calls from java to an instance of the native class using jni. This class is the java peer of the native class.

An example:

We have the print_hello class in c++ which we need to use in a java program, to do this we need to define its peer in java.

The native class

class print_hello{

public:

void do_stuff(){std::cout<

}

The peer class in java

class PrintHello{

//Address of the native instance (the native object)

long pointer;

//ctor. calls native method to create

//instance of print_hello

PrintHello(){pointer = newNative();}

//This whole class is for the following method

//which provides access to the functionality

//of the native class

public void doStuff(){do_stuff(pointer);}

//Calls a jni wrapper for print_hello.do_stuff()

//has to pass the address of the instance.

//the native keyword keeps the compiler from

//complaining about the missing method body

private native void do_stuff(long p);

//

//Methods for management of native resources.

//

//Native instance creation/destruction

private native long newNative();

private native deleteNative(long p);

//Method for manual disposal of native resources

public void dispose(){deleteNative(pointer);pointer = 0;}

}

JNI code (incomplete)

All methods declared native require a native jni implementation. The following implements only one of the native methods declared above.

//the method name is generated by the javah tool

//and is required for jni to identify it.

void JNIEXPORT Java_PrintHello_do_stuff(JNIEnv* e,jobject i, jlong pointer){

print_hello* printer = (print_hello*)pointer;

printer->do_stuff();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值