0

This is a two part question. My first question relates to boolean values in the C portion of a JNI implementation. Since C doesn't have a boolean type, I am confused about how to do this. I have a Java object that looks like this:

public class JNIResult {

    private boolean successful;
    private String data;

    public JNIResult(boolean successful, String data) {
        this.successful = successful;
        this.data = data;
    }
}

On the C side, I want to create a new JNIResult object. I have gotten the constructor method ID, and now I am calling (*env)->NewObject(). Let's say, for instance, that I want to create this object:

JNIResult(true, null);

How would I do that in C? I know that it would be something like this:

jobject JNIResult = (*env)->NewObject(env, JNIResultClass, JNIResultConstructor, true, NULL);

but the boolean thing is confusing me.

Secondly, stemming from that question, when supplying parameters to a native Java method in C, whether it's a constructor or another method, what should the type be for those values? For instance, if I am calling a Java square root function that takes a double value as an argument, on the C side should I supply a jdouble or a double value to the function?

Alex Parker
  • 1,533
  • 3
  • 16
  • 38
  • A common paradigm in C is to use integers for booleans, where zero==false and non-zero==true. You could simply change the constructor to take an int and pass 0 or 1 as appropriate. – adelphus Jul 30 '15 at 17:01
  • Yes, but I would like to keep it as boolean if possible so that I can use it as a boolean on the Java side. – Alex Parker Jul 30 '15 at 17:08
  • 2
    In JNI, Java boolean values are mapped to jboolean (which is an unsigned char) - just inlclude jni.h and pass JNI_TRUE or JNI_FALSE. – adelphus Jul 30 '15 at 17:39

1 Answers1

0

C normally uses the integer value 0 for "false" and 1 for "true". That way, the if statement will return the appropriate value.

This is explained in Using boolean values in C and the link is to the selected answer.

typedef enum { false, true } bool;

When I called a Java funtion from C, I had to create an intermediate JAVA function in order to make the appropriate changes and then use the intermediate Java function to call the java function.

Thus, instead of C calling JNIResult directly, hav it call an intermediate function in Java which will contain

boolean jbool;
typedef enum (myfalse, mytrue) mybool;

mybool cbool;
if (cbool == mytrue)
  {
    jbool = true;
  }
else
  {
    jbool = false;
  }

Now create your JNIResult object (in Java), make whatever changes need to be made before returning to C. You can use similar methods for any other situations when calling from one language to another.

@adelphus points out that JNI does the equivalent of what I show above within the JNI processing.

In JNI, Java boolean values are mapped to jboolean (which is an unsigned char) - just inlclude jni.h and pass JNI_TRUE or JNI_FALSE. – adelphus Jul 30 at 17:39

Community
  • 1
  • 1
sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • Yes, this is true, but it doesn't particularly answer my question. If the Java constructor is expecting a boolean, I can't just supply an int and make it work. – Alex Parker Jul 30 '15 at 17:12
  • @Alex I added an explanation as how I made a similar situation work in the past. – sabbahillel Jul 30 '15 at 17:30