jni 学习2

参考资料: 
http://docs.oracle.com/javase/6/docs/index.html 
http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html  java本地接口规范
官方提供的说明。 

语言的相互调用,首先要解决数据类型之间的转换。JNI提供了Java类型映射到本地C类型的转换。 
原始类型


 
引用类型




实例


public class DataType
{
static 
{
System.loadLibrary("datatype");
}

public native byte getByte(byte b);
public native short getShort(short s);
public native int getInt(int i);
public native long getLong(long lon);
public native float getFloat(float f);
public native double getDouble(double d);
public native char getChar(char c);
public native boolean getBoolean(boolean bool);
        public native void get(); 


}


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class demo_dll_DataType */


#ifndef _Included_demo_dll_DataType
#define _Included_demo_dll_DataType
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     demo_dll_DataType
 * Method:    getByte
 * Signature: (B)B
 */
JNIEXPORT jbyte JNICALL Java_demo_dll_DataType_getByte
  (JNIEnv *, jobject, jbyte b)
{
    ++b;


return b;
}








/*
 * Class:     demo_dll_DataType
 * Method:    getShort
 * Signature: (S)S
 */
JNIEXPORT jshort JNICALL Java_demo_dll_DataType_getShort
  (JNIEnv *, jobject, jshort s) 
{
    ++s;


return s;


}




/*
 * Class:     demo_dll_DataType
 * Method:    getInt
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_demo_dll_DataType_getInt
  (JNIEnv *, jobject, jint i)
{
    ++i;


return i;


}


/*
 * Class:     demo_dll_DataType
 * Method:    getLong
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_demo_dll_DataType_getLong
  (JNIEnv *, jobject, jlong lon)
{
++lon;


return lon;


}


/*
 * Class:     demo_dll_DataType
 * Method:    getFloat
 * Signature: (F)F
 */
JNIEXPORT jfloat JNICALL Java_demo_dll_DataType_getFloat
  (JNIEnv *, jobject, jfloat f)
{
    ++f;


return f;


}


/*
 * Class:     demo_dll_DataType
 * Method:    getDouble
 * Signature: (D)D
 */
JNIEXPORT jdouble JNICALL Java_demo_dll_DataType_getDouble
  (JNIEnv *, jobject, jdouble d)
{
    ++d;


return d;


}
/*
 * Class:     demo_dll_DataType
 * Method:    getChar
 * Signature: (C)C
 */
JNIEXPORT jchar JNICALL Java_demo_dll_DataType_getChar
  (JNIEnv *, jobject, jchar c)

return c; 
}


/*
 * Class:     demo_dll_DataType
 * Method:    getBoolean
 * Signature: (Z)Z
 */
JNIEXPORT jboolean JNICALL Java_demo_dll_DataType_getBoolean
  (JNIEnv *, jobject, jboolean boo)
{
    return !boo;
}


/*
 * Class:     demo_dll_DataType
 * Method:    get
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_demo_dll_DataType_get
  (JNIEnv *, jobject)
{
    printf("hello world!\n");


}


#ifdef __cplusplus
}
#endif
#endif


测试:
public class Test


public static void main(String[] args)
{
DataType dt = new DataType(); 
dt.get();
System.out.println(dt.getBoolean(true));
System.out.println(dt.getByte((byte)1));
System.out.println(dt.getChar('c'));
System.out.println(dt.getDouble(3.1234));
System.out.println(dt.getFloat(2.35F));
System.out.println(dt.getInt(123));
System.out.println(dt.getLong(456));
System.out.println(dt.getShort((short) 45)); 


}


}


结果:
false
2
c
4.1234
3.35
124
457
46
hello world!






















 









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值