Forum Stats

  • 3,839,314 Users
  • 2,262,480 Discussions
  • 7,900,930 Comments

Discussions

Best way to return primitive types to Java

573044
573044 Member Posts: 28
edited Apr 26, 2011 2:43PM in Java Native Interface (JNI)
Currently we have built classes that wrap our primitive types: MyInt, MyLong, etc. We realize another option would be to pass a single element array of the primitive types to get the data back.

Is there a standard way this should be handled? Should we use a wrapper class to get back our primitive types or by passing a primitive type array?

Also - we have some cases where we actually do need an array of ints returned. We have the same question -- should we continue to use a wrapper class that will hold the int array or just pass in the int array and get the data back out in it (but then have it also return the size returned).

Thanks!

Best Answer

  • 802316
    802316 Member Posts: 532
    Answer ✓
    Brian R wrote:
    In our case we don't just have a single value we need returned. We call our method and expect to have 2+ values returned. So are you saying in that case you build the class wrappers around the primitive objects?
    If youw ant to return multiple results a common pattern is to pass an array which you can populate and optionally a length to say how many values were returned. e.g.
    public int read(byte[] bytes);
    Such a method can populate any number of bytes (up to its length) and it returns the number of bytes populated. read() often returns -1 to indicate no more data is available.

    You can do the same with an int array, or a long arrary. If you don't want to return the length, you can use the first or last element to hold the length. e.g.
    public void populate(byte[] bytes, int[] ints, long[] longs);

Answers

  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    This question doesn't really have any to to with JNI.

    Exactly the same question can be applied to a regular java method.

    Myself if I am going to return a primitive (singular) from a java method then I do exactly that.

    Only reason to not do that is in fact I am not just returning a primitive but actually want to return other 'information' as well.
    jschellSomeoneStoleMyAlias
  • 573044
    573044 Member Posts: 28
    Good point - I see how this can apply to Java development in general.

    In our case we don't just have a single value we need returned. We call our method and expect to have 2+ values returned. So are you saying in that case you build the class wrappers around the primitive objects?

    Thanks again!
  • 573044
    573044 Member Posts: 28
    or - instead of using custom wrappers (myInt, myLong, etc) just use the built-in ones (Integer, Long, etc)?
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Apr 26, 2011 4:51AM
    Why any wrappers? Just an additional complication. Just return primitive types. If the Java code wants an autoboxed result, let it happen there in the Java code. No need to do the compiler's work for it in your JNI code.
    EJP
  • 802316
    802316 Member Posts: 532
    Answer ✓
    Brian R wrote:
    In our case we don't just have a single value we need returned. We call our method and expect to have 2+ values returned. So are you saying in that case you build the class wrappers around the primitive objects?
    If youw ant to return multiple results a common pattern is to pass an array which you can populate and optionally a length to say how many values were returned. e.g.
    public int read(byte[] bytes);
    Such a method can populate any number of bytes (up to its length) and it returns the number of bytes populated. read() often returns -1 to indicate no more data is available.

    You can do the same with an int array, or a long arrary. If you don't want to return the length, you can use the first or last element to hold the length. e.g.
    public void populate(byte[] bytes, int[] ints, long[] longs);
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    Brian R wrote:
    In our case we don't just have a single value we need returned. We call our method and expect to have 2+ values returned. So are you saying in that case you build the class wrappers around the primitive objects?
    You can return an object - data members are values.
    You can return an array, int array or even object array.
    You can pass in an array, on exit is it is set.
    You can pass in an object, on exit it is set.
This discussion has been closed.