• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting "int" when calls for jint?

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. But are all OS's guaranteed to be the same?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic