翻译自https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
Chapter 3
Primitive Types
Table 3-1 描述Java基本类型及其与机器相关的本地代码的等价类型。
Table 3-1 Primitive Types and Native Equivalents
#define JNI_FALSE 0
#define JNI_TRUE 1
typedef jint jsize;
Reference Types
JNI包含一系列的对应于不同种类的Java对象的引用类型。 JNI引用类型按照Figure 3-1.所示的层次结构进行组织。
Figure 3-1 Reference Type Hierarchy
在C语言中,所有其他JNI引用类型的定义与jobject的定义都相同。 例如:
typedef jobject jclass;
以下摘自jni.h中,C部分定义:
/*
* Reference types, in C.
*/
typedef void* jobject;
typedef jobject jclass;
typedef jobject jstring;
typedef jobject jarray;
typedef jarray jobjectArray;
typedef jarray jbooleanArray;
typedef jarray jbyteArray;
typedef jarray jcharArray;
typedef jarray jshortArray;
typedef jarray jintArray;
typedef jarray jlongArray;
typedef jarray jfloatArray;
typedef jarray jdoubleArray;
typedef jobject jthrowable;
typedef jobject jweak;
在C ++中,JNI引入了一组伪类来加强子类型关系。 例如:
class _jobject {};
class _jclass : public _jobject {};
...
typedef _jobject *jobject;
typedef _jclass *jclass;
以下摘自jni.h中,C++部分定义:
/*
* Reference types, in C++
*/
class _jobject {};
class _jclass : public _jobject {};
class _jstring : public _jobject {};
class _jarray : public _jobject {};
class _jobjectArray : public _jarray {};
class _jbooleanArray : public _jarray {};
class _jbyteArray : public _jarray {};
class _jcharArray : public _jarray {};
class _jshortArray : public _jarray {};
class _jintArray : public _jarray {};
class _jlongArray : public _jarray {};
class _jfloatArray : public _jarray {};
class _jdoubleArray : public _jarray {};
class _jthrowable : public _jobject {};
typedef _jobject* jobject;
typedef _jclass* jclass;
typedef _jstring* jstring;
typedef _jarray* jarray;
typedef _jobjectArray* jobjectArray;
typedef _jbooleanArray* jbooleanArray;
typedef _jbyteArray* jbyteArray;
typedef _jcharArray* jcharArray;
typedef _jshortArray* jshortArray;
typedef _jintArray* jintArray;
typedef _jlongArray* jlongArray;
typedef _jfloatArray* jfloatArray;
typedef _jdoubleArray* jdoubleArray;
typedef _jthrowable* jthrowable;
typedef _jobject* jweak;
Field and Method IDs
方法和字段ID是常规的C指针类型:
struct _jfieldID; /* opaque structure */
typedef struct _jfieldID* jfieldID; /* field IDs */
struct _jmethodID; /* opaque structure */
typedef struct _jmethodID* jmethodID; /* method IDs */
The Value Type
jvalue联合类型被用作参数数组中的元素类型。 声明如下:
typedef union jvalue {
jboolean z;
jbyte b;
jchar c;
jshort s;
jint i;
jlong j;
jfloat f;
jdouble d;
jobject l;
} jvalue;
Type Signatures
JNI使用Java VM的类型签名表示。 Table 3-2显示了这些类型签名。
Table 3-2 Java VM Type Signatures
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
Modified UTF-8 Strings(改良的UTF-8字符串?)
The JNI uses modified UTF-8 strings to represent various string types. Modified UTF-8 strings are the same as those used by the Java VM. Modified UTF-8 strings are encoded so that character sequences that contain only non-null ASCII characters can be represented using only one byte per character, but all Unicode characters can be represented.
All characters in the range \u0001 to \u007F are represented by a single byte, as follows:
JNI使用改良的UTF-8字符串来表示各种字符串类型。 改良的UTF-8字符串与Java VM使用的字符串相同。 改良的UTF-8字符串经过编码,对于仅包含非空ASCII字符的字符序列的每个字符都只用一个字节表示,但是可以表示所有Unicode字符。
\ u0001到\ u007F范围内的所有字符都由一个字节表示,如下所示:
0 | bits 6-0 |
字节中的七个数据位表示所表示字符的值。
空字符('\ u0000')和范围从'\ u0080'到'\ u07FF'的字符由一对字节x和y表示:
x:
1 | 1 | 0 | bits 10-6 |
y:
1 | 0 | bits 5-0 |
The bytes represent the character with the value ((x & 0x1f
) << 6
) + (y & 0x3f
).
'\ u0800'到'\ uFFFF'范围内的字符由3个字节x,y和z表示:
x:
1 | 1 | 1 | 0 | bits 15-12 |
y:
1 | 0 | bits 11-6 |
z:
1 | 0 | bits 5-0 |
The character with the value ((x & 0xf
) << 12
) + ((y & 0x3f
) << 6
) + (z & 0x3f
) is represented by the bytes.
Characters with code points above U+FFFF (so-called supplementary characters) are represented by separately encoding the two surrogate code units of their UTF-16 representation. Each of the surrogate code units is represented by three bytes. This means, supplementary characters are represented by six bytes, u, v, w, x, y, and z:
大于U + FFFF的字符(所谓的补充字符)可以用两个UTF-16形式的代理代码单元表示。 每个代理代码单元由三个字节表示。 这意味着,补充字符由u,v,w,x,y和z六个字节表示:
u:
1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
v:
1 | 0 | 1 | 0 | (bits 20-16) - 1 |
w:
1 | 0 | bits 15-10 |
x:
1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
y:
1 | 0 | 1 | 1 | bits 9-6 |
z:
1 | 0 | bits 5-0 |
The character with the value 0x10000+((v&0x0f)<<16)+((w&0x3f)<<10)+(y&0x0f)<<6)+(z&0x3f) is represented by the six bytes.
多字节字符的字节以big-endian(高字节在前)的顺序存储在类文件中。
此格式与标准UTF-8格式有两个区别。 首先,空字符(char)0使用两字节格式而不是一字节格式进行编码。 这意味着修改后的UTF-8字符串永远不会嵌入空值null。 其次,标准UTF-8有一字节,两字节和三字节格式被使用。 Java VM无法识别四字节格式的标准UTF-8。 它使用自己的2乘3字节格式替代。
有关标准UTF-8格式的更多信息,请参见Unicode标准版本4.0的3.9 Unicode编码形式。