Reputation: 554
chapter 3 of the book "the Java Native Interface Programmer’s Guide and Specification",which says "The mapping of primitive types is straightforward",I'm a little confused about the sentence. As we know that the size of c data types is uncertain,it depends on the environment where the program run ,complie... such as the size of int type may be 2 bytes or 4 bytes. But the size of java data types is definite.the size of int is 4 bytes.
so,how does jni map the data types between java and c/c++ clearly.
any answers will be appreciated!
Upvotes: 2
Views: 490
Reputation: 52343
The jni.h header defines appropriate primitive types: jint, jlong, jchar, and so on. These are declared to match the sizes of the corresponding Java primitives. Use these in your JNI code.
Upvotes: 1
Reputation: 88418
Well, in C, there is a module called stdint.h
which defines the types int64_t
, int32_t
, and friends, which does make the mapping "straightforward."
But you're right to note that in any particular implementation of C, the size of plain old int
is not specified exactly. The author was indeed pretty loose with the word "straightforward," and was assuming you would read the claim as something like "Java's int
maps to the C signed integer type of 32 bits."
Upvotes: 1