Wrapper.java

1
2
3
4
5
6
7
8
9
10
11
12
class Wrapper{
    static {
        System.loadLibrary("jni_type"); //libjni_type.so
    }
    
    public native void print(boolean b); //function overloading. 
                                         //one interface multi method
    public native void print(char c);
    public native void print(byte b, short s, int i, long l);
    public native void print(float f, double d);
    
}




Main.java


1
2
3
4
5
6
7
8
9
10
11
class Main{
    public static void main(String [] args)
    {
        Wrapper wrapper = new Wrapper();
        wrapper.print(true);    // true, false
        wrapper.print('A');
        wrapper.print( (byte)10, (short)20, 30, 40L);// C case int a = 3.14; 
                                      // Java case int a = (int) 3.14;
        wrapper.print( 3.1415F, 2.718);
    }
}





jni_type.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include "Wrapper.h"
 
JNIEXPORT void JNICALL Java_Wrapper_print__Z(JNIEnv *env, jobject obj, jboolean b)
{
    printf("boolean --> unsigned char : %s\n", (b) ? "true" : "false"); // if(b==true) return "turn"
}                                                                    // if(b==false) return "false"        
 
JNIEXPORT void JNICALL Java_Wrapper_print__C(JNIEnv *env, jobject obj, jchar c)
{
    printf("char --> unsigned short int : %c\n", (unsigned char)c);    // 0 to 255s
}
 
JNIEXPORT void JNICALL Java_Wrapper_print__BSIJ(JNIEnv *env, jobject obj, jbyte b, jshort s, jint i, jlong l)
{
    printf("byte --> signed char : %d\n", b);
    printf("short --> short int : %d\n", s);
    printf("int --> int : %d\n", i);
    printf("long --> long long int : %lld\n", l);
}
 
JNIEXPORT void JNICALL Java_Wrapper_print__FD(JNIEnv *env, jobject obj, jfloat f, jdouble d)
{
    printf("float --> float : %.2f\n", f);
    printf("double --> double : %.2f\n", d);
 
 
}





Posted by 차희빈

차희빈

달력