• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting "int" when calls for jint?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am programming in C++ and I have an "int" that I need to send to a java class that calls for a jint, do I need to do any conversion or is an int always castable to a jint on every OS?

for example:


Or do I need to do something with the "int" to ensure it is the proper size?
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From jni_md.h from a Linux machine:

So technically, you don't need to do anything except cast.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But are all OS's guaranteed to be the same?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, remember that (for example) an "int" in C isn't always the same size, unlike in Java. So for different compilers, the C type corresponding to jint could, indeed, be different. But "jint" will always be defined as a 32-bit signed integer type -- or at least, as an integer type 32 bits or larger, with only the 32 least significant bits significant. You have similar assurances about the other primitive types. A cast should always be sufficient. Use "jint" yourself, when you can, for declarations -- i.e., write

jint x = 5;

in your JNI code.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Well, remember that (for example) an "int" in C isn't always the same size, unlike in Java. So for different compilers, the C type corresponding to jint could, indeed, be different. But "jint" will always be defined as a 32-bit signed integer type -- or at least, as an integer type 32 bits or larger, with only the 32 least significant bits significant. You have similar assurances about the other primitive types. A cast should always be sufficient. Use "jint" yourself, when you can, for declarations -- i.e., write

jint x = 5;

in your JNI code.


Right, but what if I'm calling a "C" method that returns an "int"? Are you saying that simply casting will never lose bits or any other weird bit problems occurring?

Thanks!
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On linux, jint is int but on windows it's a long. (And a long is an __int64). So I just want to make sure that I'm not losing any data. I haven't seen any methods in JNI for converting a C++ int to a jint (which could be important seeing as they're diff. on Linux versus windows). Anyone know of any?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a C library function that returns an "int", you will need to find out what size the data could really be, given the semantics of that function.

Plenty of methods that declare they return an "int" are in fact never likely to return values even approaching 16 bits, let alone 32. For instance, a function called getNumberOfWheelsOnWagon() is never going to return several million, but might be declared to return "int" all the same.

But if there is a chance that it won't fit in 32 bits, you cannot store it safely in a "jint".

There is, of course, no magic method in JNI or anywhere that can make more than 32 bits fit in 32 bits.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!