JNI中数据类型的意义在于桥接Java数据类型与C数据类型。
简单数据类型:
Java Type | Native Type | Description |
---|---|---|
boolean | jboolean | unsigned 8 bits |
byte | jbyte | signed 8 bits |
char | jchar | unsigned 16 bits |
short | jshort | signed 16 bits |
int | jint | signed 32 bits |
long | jlong | signed 64 bits |
float | jfloat | 32 bits |
double | jdouble | 64 bits |
void | void | N/A |
引用类型:
特征类型(Signatures):
看到Signatures是不是有点眼熟,在我的Android JNI之静态注册中一带而过的javah自动生成头文件,注意看javah不仅完成了方法名的转换,而且在方法注释中标注了该方法的Signature,下面说一下这个对应关系。
Type Signature | Java Type |
---|---|
Z | boolean |
B | byte |
C | char |
S | short |
I | int |
J | long |
F | float |
D | double |
L fully-qualified-class ; | fully-qualified-class |
[ type | type[] |
( arg-types ) ret-type | method type |
举个例子,对于Java方法
long f (int n, String s, int[] arr);
其对应的特征值就是(ILjava/lang/String;[I)J
PS:这个特征值在动态注册的时候很重要,这关系到你在Java类中声明的native方法能否正确找到对应的native方法