Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Best way to return primitive types to Java

573044
Member Posts: 28
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!
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
-
Brian R wrote: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.
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?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
-
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. -
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! -
or - instead of using custom wrappers (myInt, myLong, etc) just use the built-in ones (Integer, Long, etc)?
-
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.
-
Brian R wrote: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.
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?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);
-
Brian R wrote:You can return an object - data members are values.
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 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.