Write Efficient Java Apps Using Native Data Structures with JNI
Sometimes Java's data structures use too much memory to store the data you need to store. In such situations, you can use the JNI native code interface to access native data structures. Find out how to use the STL in C++ to implement a space-efficient hashtable that works like a regular Java hashtable.
by Greg Travis
August 27, 2004
he Java Native Interface (JNI) is used to call code written in another languagesuch as C or C++from a Java program. As such, it is often used when a Java program needs to call pre-existing code in another language or to call code that could be written in Java but needs to be written in C/C++ for some reason. Data structures implemented in C or C++ can often be fasterand use less spacethan the equivalent data structures in Java.
This article investigates the use of the JNI for accessing a data structure implemented in C++. The data structure I'll be implementing is a hashtable called JNativeHash. It's implementation is based on the map class from the Standard Template Libraries (STL). Unlike Java, C++ does not emphasize the use of run-time type identification, so it's not as easy to create a hashtable that maps arbitrary objects to arbitrary objects. Instead, I'll be creating a hashtable that maps strings to strings.
It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com. Already a member?
To become a member of DevX.com create your Member Profile by completing the form below. Membership is free!