| 
 | 
| When passing numbers between C and the Java programming language, you should understand which types correspond to each other. For example, although C does have data types called int and long, their implementation is platform dependent. On some platforms, ints are 16-bit quantities, and on others they are 32-bit quantities. In the Java platform, of course, an int is always a 32-bit integer. For that reason, the Java Native Interface defines types jint, jlong, and so on. Table 11-1 shows the correspondence between Java types and C types. 
 In the header file jni.h, these types are declared with typedef statements as the equivalent types on the target platform. That header file also defines the constants JNI_FALSE = 0 and JNI_TRUE = 1. Using printf for Formatting NumbersUntil JDK 5.0, Java had no direct analog to the C printf function. Let's suppose you are stuck with an older JDK release and decide to implement the same functionality by calling the C printf function in a native method. Example 11-5 shows a class called Printf1 that uses a native method to print a floating-point number with a given field width and precision. Example 11-5. Printf1.java 1. class Printf1 2. { 3.    public static native int print(int width, int precision, double x); 4. 5.    static 6.    { 7.       System.loadLibrary("Printf1"); 8.    } 9. } Notice that when the method is implemented in C, all int and double parameters are changed to jint and jdouble, as shown in Example 11-6. Example 11-6. Printf1.c  1. #include "Printf1.h"  2. #include <stdio.h>  3.  4. JNIEXPORT jint JNICALL Java_Printf1_print(JNIEnv* env, jclass cl,  5.    jint width, jint precision, jdouble x)  6. {  7.    char fmt[30];  8.    jint ret;  9.    sprintf(fmt, "%%%d.%df", width, precision); 10.    ret = printf(fmt, x); 11.    fflush(stdout); 12.    return ret; 13. } The function simply assembles a format string "%w.pf" in the variable fmt, then calls printf. It then returns the number of characters printed. Example 11-7 shows the test program that demonstrates the Printf1 class. Example 11-7. Printf1Test.java  1. class Printf1Test  2. {  3.    public static void main(String[] args)  4.    {  5.       int count = Printf1.print(8, 4, 3.14);  6.       count += Printf1.print(8, 4, count);  7.       System.out.println();  8.       for (int i = 0; i < count; i++)  9.          System.out.print("-"); 10.       System.out.println(); 11.    } 12. }  | 
| 
 | 
