55

While there is documentation regarding turning a jstring to a native string (string nativeString = env->GetStringUTFChars(jStringVariable, NULL);) I can't find an example which will convert a jboolean to a bool or a jint to an int.

Can anyone suggest how this is achieved?

Yuchen
  • 30,852
  • 26
  • 164
  • 234
Graeme
  • 25,714
  • 24
  • 124
  • 186

6 Answers6

66

You just need to cast jintto int using C style casts. Same for jboolean to bool (if you're using C99 bool type) or to uint8_t (if you're using std int types) or to unsigned char.

Open $NDK_ROOT/platforms/android-8/arch-arm/usr/include/jni.h and you'll see jint, jboolean etc are just typedefs.

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
30

To cast a jboolean (which may only contain the values JNI_FALSE or JNI_TRUE) to a native bool I would use something like this :

(bool)(jboolean == JNI_TRUE)

If perhaps the jboolean isn't coming from the JVM, then testing for jboolean != JNI_FALSE might be considered safer.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 9
    That's a little limiting. I think (bool)(java_rtn != JNI_FALSE) is better because some libraries may return true as some integer not equal to 1 (and JNI_TRUE == 1). – greg7gkb Dec 10 '12 at 06:13
  • 1
    @greg7gkb umm, whut?! What "library" are you talking about? The JVM should guarantee that a `boolean` passed from Java to C will be exactly 0 or 1. – Alnitak Feb 24 '15 at 11:33
  • 1
    @Alnitak Most functions of the Windows API returns 0 if there was an error, and ANY VALUE OTHER THAN 0 if the function succeeded. So, a function can succeed and return a value, for example, of 28, and using the code of this answer, it will be converted to false. – João Vitor Verona Biazibetti Nov 14 '15 at 21:03
  • 4
    @BloodShura the question was about converting _from_ Java's `jboolean` to native, not the other way around, so what the Windows API might do here is irrelevant. – Alnitak Nov 15 '15 at 02:29
  • 2
    @Alnitak The problem is that, not always the jboolean's will come directly from the JVM. You can cast a bool type (that can come from any other source like, for example, the Windows APIs) to a jboolean, and then, it is possible to this jboolean have more values than only 0s and 1s. Then, for the sake of security, it is better to check if the value is different than zero, rather than checking equality to one. – João Vitor Verona Biazibetti Nov 15 '15 at 02:33
3

Same issue–fixed. In my case I'm using openFrameworks so I don't know if this applies to non-openFrameworks projects (haven't tested). However, it appears that the first two arguments in an external function are always "env" and "thiz" and these need to be defined explicitly for each new extern function.

extern "C"{

// casts the variable properly
void Java_com_package_JavaClass_someFunction( JNIEnv*  env, jobject  thiz, jboolean yourBool ){
    myTestApp->someFunction( (bool) yourBool );
}

// "yourBool" will always be "1" because its taking the spot of "thiz" which is not null
void Java_com_package_JavaClass_someFunction( JNIEnv*  env, jboolean yourBool ){
    myTestApp->someFunction( (bool) yourBool );
}

// "yourBool" will always be "1" because its taking the spot of "env" which is not null
void Java_com_package_JavaClass_someFunction( jboolean yourBool ){
    myTestApp->someFunction( (bool) yourBool );
}


}
Steven
  • 31
  • 1
2

The odd one out is jchar. It's defined as unsigned short, and depending on your compilation settings, that may or may not be equivalent to wchar_t. Depending on your underlying platform, you may be better off working with UTF8 strings. At least those are bitwise equivalent to ASCII for the ASCII subset of characters.

On Windows and Mac OS/Cocoa, however, the native wide string representation is exactly unsigned short. Java strings fit naturally into that.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

If you just want to use it in if(..) statement, this is working for me without conversion:

if (jboolean == true) {
  return true;
} else {
  return false;
}
oldmud0
  • 147
  • 2
  • 11
zajac.m2
  • 1,218
  • 14
  • 13
0

This has worked for me:

bool isValue = bool(jbooleanValue);
narcis dpr
  • 939
  • 2
  • 12
  • 32
  • Adding some commentary why this answers the OP's question will help future people trying to find answers to similar questions. – SiKing Sep 24 '20 at 19:08