import java.util.Vector;
class ObjParam
{
// passing a Vector containing Double objects
public native double sum(Vector v);
static {
System.loadLibrary("ObjParam");
}
public static void main(String[] args) {
ObjParam app = new ObjParam();
Vector v = new Vector();
v.addElement(new Double(3.14));
v.addElement(new Double(19.2));
v.addElement(new Double(6.7));
System.out.println("result is " + app.sum(v));
}
}
/*
ObjParam.cpp
#include <iostream.h>
#include "ObjParam.h"
JNIEXPORT jdouble JNICALL Java_ObjParam_sum
(JNIEnv *env, jobject obj, jobject vector) {
jclass vectorClass = env->FindClass("java/util/Vector");
jclass doubleClass = env->FindClass("java/lang/Double");
jmethodID vectorSize = env->GetMethodID(vectorClass, "size", "()I");
int size = env->CallIntMethod(vector, vectorSize);
cout << "size of Vector is " << size << endl;
jmethodID vectorElementAt =
env->GetMethodID(vectorClass, "elementAt", "(I)Ljava/lang/Object;");
jmethodID doubleDoubleValue =
env->GetMethodID(doubleClass, "doubleValue", "()D");
jdouble sum = 0;
jobject object;
jdouble value;
for (int i = 0; i < size; ++i) {
cout << "calling elementAt(" << i << ")" << endl;
object = env->CallObjectMethod(vector, vectorElementAt, i);
value = env->CallDoubleMethod(object, doubleDoubleValue);
cout << "value = " << value << endl;
sum += value;
}
return sum;
}
*/
/*
size of Vector is 3
calling elementAt(0)
value = 3.14
calling elementAt(1)
value = 19.2
calling elementAt(2)
value = 6.7
result is 29.04
*/
public class PrimParam {
public native double takePrimParam(int i, double d, String s);
static {
System.loadLibrary("PrimParam");
}
public static void main(String[] args) {
PrimParam app = new PrimParam();
System.out.println
("result is " + app.takePrimParam(19, 3.14, "Duke"));
}
}
/*
#include "stdafx.h"
#include "..\PrimParam.h"
#include <iostream>
JNIEXPORT jdouble JNICALL Java_PrimParam_takePrimParam
(JNIEnv * env, jobject, jint i, jdouble d, jstring s)
{
std::cout << "i=" << i
<< ", d=" << d
<< ", s=" << env->GetStringUTFChars(s, 0)
<< std::endl;
return i + d;
}
*/
/*
i=19, d=3.14, s=Duke
result is 22.14
请按任意键继续. . .
*/