3) 编写调用native方法的Java测试代码:
1 public class MyTest {
2 public static void main(String[] args) throws Exception {
3 double price = 44.95;
4 double tax = 7.75;
5 double amountDue = price * (1 + tax / 100);
6 String s = Printf.print(\"Amount due = %8.2f\", amountDue);
7 System.out.println(s);
8 }
9 }
10 /* 输出结果如下:
11 Amount due = 48.43
12 */
4. 带有中文的字符串参数的本地方法:
1) 编写带有native方法的Java代码: 该代码和上面示例中的代码完全相同。
2) 编写与native方法对应的C语言代码:
1 #include \"Printf.h\"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <float.h>
5 #include <wchar.h>
6
7 JNIEXPORT jstring JNICALL Java_Printf_print
8 (JNIEnv* env, jclass cl, jstring format, jdouble x)
9 {
10 //1. 当传递的字符串中包含中文等unicode字符时,推荐使用GetStringChars这一组函数,
11 //当只是包含ASCII的时候,推荐使用上面例子中的GetStringUTFChars那一组函数。
12 //2. 返回Unicode编码的字符串的指针,或者当不能构建字符数组时返回NULL。
13 //直到RelaseStringChars函数调用前,该指针一致有效。
14 //3. 由于Java针对字符串使用了对象池的技术,因此这里一定不能修改返回的const jchar*
15 const jchar* wformat = env->GetStringChars(format,NULL);
16 jchar* wret = (jchar*)calloc(env->GetStringLength(format) + 20,sizeof(jchar));
17 swprintf((wchar_t*)wret,(const wchar_t*)wformat,x);
18 size_t length = wcslen((wchar_t*)wret);
19 //4. 这里需要用JNI提供的NewString方法将本地的unicode字符串构造成jstring。
20 //和Java进行字符串交互时只能使用jstring。
21 jstring ret = env->NewString(wret,length);
22 free(wret);
23 //5. 这里需要手工调用ReleaseStringChars方法,以便Java的垃圾收集器可以回收该资源。