开发者

Type casting a variable from jBoolean to bool

开发者 https://www.devze.com 2023-03-12 04:50 出处:网络
When using JNI to interface between Java and C, javah parses a boolean value in Java to jBoolean in the JNI header file. When I use bool in the C f开发者_如何学Pythonile, the Visual studio compiler th

When using JNI to interface between Java and C, javah parses a boolean value in Java to jBoolean in the JNI header file. When I use bool in the C f开发者_如何学Pythonile, the Visual studio compiler throws a warning that

warning C4800: 'jboolean' : forcing value to bool 'true' or 'false' (performance warning)

Is there any other data type that should be used? Or if bool is the only data type here, what exactly are the performance problems that I might face?


The "performance problem" is that the cast isn't completely free. Casting to bool essentially means forcing all non-zero values to 1, which takes a tiny bit of code, as opposed to a "free" cast like widening char to int. The "performance problem" is a few extra machine instructions. If you're doing this a million times in a tight loop, OK, maybe you care -- otherwise, no, you shouldn't care at all. This is IMO a silly compiler warning; that small added cost is simply part of the compromise you make when using bool and the compiler shouldn't be bothering you about it.


Try casting to BOOL instead. You might find this thread interesting too: Difference between bool and BOOL.

I'm not quite sure this is what you are looking for however. BOOL is 16 bits, not 8 bits. If that is okay then it is closer to what you're looking for than bool. Otherwise you could use byte. Note if you do end up casting to any of these you won't be able to do == true anymore. This is also explained in the link I provided.

0

精彩评论

暂无评论...
验证码 换一张
取 消