<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JNI | Student Projects</title>
	<atom:link href="https://studentprojects.in/category/software-development/jni/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 05:20:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>JNI Part 5: JNI Arrays</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Mon, 22 Aug 2011 04:48:41 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[SetIntArrayRegion]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[jni]]></category>
		<category><![CDATA[ReleaseIntArrayElements]]></category>
		<category><![CDATA[jintarray]]></category>
		<category><![CDATA[jchararray]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1688</guid>

					<description><![CDATA[<p>Arrays have dimension. An array’s dimension determines the number of indexes needed to access an element. The standard convention for declaring arrays is: String[] s; // one-dimensional array String s[]; // one-dimensional array String[][] s; // two-dimensional array Declaring the size of the array with the following notation is illegal: String[5] s; // illegal declaration</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/">JNI Part 5: JNI Arrays</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<ol>
<li>Arrays have dimension. An array’s dimension determines the number of indexes needed to access an element.</li>
<li>The standard convention for declaring arrays is:
<ul>
<li>String[] s; // one-dimensional array</li>
<li>String s[]; // one-dimensional array</li>
<li>String[][] s; // two-dimensional array</li>
</ul>
</li>
<li>Declaring the size of the array with the following notation is illegal:
<ul>
<li>String[5] s; // illegal declaration</li>
</ul>
</li>
<li>An array implicitly extends java.lang.Object. Therefore, an array is an instance of Object class. Hence, arrays in Java are objects.</li>
<li>Declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array.</li>
<li>Since arrays are Objects they can be initialized using the new operator.</li>
<li>When created, arrays are automatically initialized with the default value of their type.
<ul>
<li>String[] s = new String[100];   // default values: null</li>
<li>boolean[] b = new boolean[4]; // default values: false</li>
<li>int[][] i = new int[10][10];        // default values: 0</li>
</ul>
</li>
<li>The JNI treats primitive arrays and object arrays differently.</li>
<li>Primitive arrays contain elements that are of primitive types such as int and boolean.</li>
<li>Object arrays contain elements that are of reference types such as class instances and other arrays.
<ul>
<li>int[] iarr;</li>
<li>float[] farr;</li>
<li>Object[] oarr;</li>
<li>int[][] arr2;</li>
</ul>
</li>
<li>Accessing primitive arrays in a native method requires the use of JNI functions similar to those used for accessing strings.</li>
<li>The JNI Get&lt;Type&gt;ArrayRegion function copies all the elements in the JNI array into a C buffer of an appropriate type. The second argument is the starting index of the elements, and the third argument is the number of elements to be copied. Once the elements are in the C buffer, we can access them in native code.
<ul>
<li>Example: env-&gt;GetCharArrayRegion(arr, 0, arrLength, chArrRWB);</li>
</ul>
</li>
<li>The JNI also supports a corresponding Set&lt;Type&gt;ArrayRegion function that allows native code to modify the array elements. Arrays of all the primitive types (such as boolean, short, and float) are also supported.</li>
<li>The JNI supports a family of Get/Release&lt;Type&gt;ArrayElements functions (for example, Get/ReleaseIntArrayElements) that allow the native code to obtain a direct pointer to the elements of primitive arrays.</li>
<li>The GetArrayLength function returns the number of elements in primitive or object arrays.</li>
</ol>
<p><strong>Example: Accessing intarray</strong></p>
<pre lang="c" line="1" escaped="true">JNIExport jint JNICALL Java_IntArray_sumArray(JNIEnv *env, jobject obj,
	jintArray arr){
		jsize len = *env-&gt;GetArrayLength(env,arr);
		jint *body = *env-&gt;GetIntArrayElements(env,arr,0);
		for (jint i = 0; i &lt; len; ++i){
			sum += body[i];
		}
	*env-&gt;ReleastIntArrayElements(env, arr, body, 0);
}</pre>
<table class="tbl" border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="30%"><strong>JNI Function</strong></td>
<td width="70%"><strong>Description</strong></td>
</tr>
<tr>
<td>Get&lt;Type&gt;ArrayRegion<br />
Set&lt;Type&gt;ArrayRegion</td>
<td>Copies the contents of primitive arrays to or from a pre-allocated C buffer.</td>
</tr>
<tr>
<td>Get&lt;Type&gt;ArrayElements<br />
Release&lt;Type&gt;ArrayElements</td>
<td>Obtains a pointer to the contents of a primitive array. May return a copy of the array.</td>
</tr>
<tr>
<td>GetArrayLength</td>
<td>Returns the number of elements in the array.</td>
</tr>
<tr>
<td>New&lt;Type&gt;Array</td>
<td>Creates an array with the given length.</td>
</tr>
<tr>
<td>GetPrimitiveArrayCritical<br />
ReleasePrimitiveArrayCritical</td>
<td>Obtains or releases a pointer to the contents of a primitive array. These functions allow virtual machines to disable garbage collection while the native code accesses the contents of primitive arrays.</td>
</tr>
</tbody>
</table>
<p><strong>JNI Array Example program</strong></p>
<p>Let’s write a JNI application that passes an unsorted integer array to a native method, which method sorts and sends it back to the application.</p>
<p>The Java code will be(SortArray.java):</p>
<pre lang="java" line="1" escaped="true">class SortArray {
	private native int[] sort(int[] arr);  //native method 

	static   //static initializer code
	{
		System.loadLibrary("SortArray");
	} 

	public static void main(String[] args)
	{
		int iArr[] = {4,5,2,7,1,9}; // Input array
		int oArr[]; //Output array

		SortArray arr = new SortArray();

		System.out.println("Unsorted array: ");
		for(int i = 0; i &lt; iArr.length; i++){
			System.out.println(iArr[i] + " ");
		}

		oArr = arr.sort(iArr);

		System.out.println("Sorted array: ");
		for(int i = 0; i &lt; oArr.length; i++){
			System.out.println(oArr[i] + " ");
		}
	}
}</pre>
<p>Compile SortArray.java (javac SortArray.java) and generate hedder file(javah -jni SortArray).</p>
<p>The generated with javah header file SortArray.h will be:</p>
<pre lang="c" line="1" escaped="true">/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class SortArray */

#ifndef _Included_SortArray
#define _Included_SortArray
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     SortArray
 * Method:    sort
 * Signature: ([I)[I
 */
JNIEXPORT jintArray JNICALL Java_SortArray_sort
  (JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif</pre>
<p>The C++ implementation file SortArray.cpp will be:</p>
<pre lang="cpp" line="1" escaped="true">#include "SortArray.h"
#include "jni.h"

/*
 * Class:     SortArray
 * Method:    sort
 * Signature: ([I)[I
 */
JNIEXPORT jintArray JNICALL Java_SortArray_sort
    (JNIEnv *env, jobject obj, jintArray arr){

    jsize arrLength = env-&gt;GetArrayLength(arr);
    jintArray arrSorted = env-&gt;NewIntArray(arrLength); 

    jint *arrOut = NULL;
    arrOut = env-&gt;GetIntArrayElements(arr, 0);

    for(jsize x = 0; x &lt; arrLength; x++){
        for(jsize y = 0; y &lt; arrLength - 1; y++){
                        if(arrOut[y] &gt; arrOut[y+1]){
				jsize temp = arrOut[y+1];
				arrOut[y+1] = arrOut[y];
				arrOut[y] = temp;
			}
		}
	}

    env-&gt;SetIntArrayRegion(arrSorted, 0, arrLength, arrOut);
    env-&gt;ReleaseIntArrayElements(arr, arrOut, 0);

    return arrSorted;
}</pre>
<p>Run the program (java SortArray) and verify the output.</p><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/">JNI Part 5: JNI Arrays</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>JNI Part 4: JNI Strings</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Mon, 22 Aug 2011 04:45:47 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[jni string funtions]]></category>
		<category><![CDATA[jni string example]]></category>
		<category><![CDATA[JNI strings]]></category>
		<category><![CDATA[NewStringUTF]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1682</guid>

					<description><![CDATA[<p>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *). We cannot use a jstring as a normal C string. We must use the appropriate JNI functions to convert jstring objects to C/C++ strings. The JNI supports conversion both to</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/">JNI Part 4: JNI Strings</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<ol>
<li>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *).</li>
<li>We cannot use a jstring as a normal C string.</li>
<li>We must use the appropriate JNI functions to convert jstring objects to C/C++ strings.</li>
<li>The JNI supports conversion both to and from Unicode and UTF-8 strings.</li>
<li>Unicode strings represent characters as 16-bit values, whereas UTF-8 strings use an encoding scheme that is upward compatible with 7-bit ASCII strings.</li>
<li>UTF-8 strings act like NULL-terminated C strings.</li>
</ol>
<p><strong>Some JNI String Functions</strong></p>
<ol>
<li>The GetStringUTFChars function is available through the JNIEnv interface pointer. It converts the jstring reference, typically represented by the Java virtual machine implementation as a Unicode sequence, into a C string represented in the UTF-8 format.</li>
<li>The ReleaseStringUTFChars frees the memory used for native string resources. Therefore, calling this function will free the memory taken by the UTF-8 string. Failure to call ReleaseStringUTFChars would result in a memory leak.</li>
<li>The NewStringUTF function constructs a new java.lang.String instance in the native method. The NewStringUTF function takes a C string with the UTF-8 format and constructs a java.lang.String instance. The newly constructed java.lang.String instance represents the same sequence of Unicode characters as the given UTF-8 C string.</li>
</ol>
<p>To convert a jstring to a C-style string, you might write code like the following:</p>
<pre lang="c" line="1" escaped="true">JNIEXPORT void JNICALLJava_MyJavaClass_printName(JNIEnv *env, jobject obj,
	jstring name)
{
	const char *str= (*env)-&gt;GetStringUTFChars(env,name,0);
	printf(“%s”, str);
	//need to release this string when done with it in
	//order to avoid memory leak
	(*env)-&gt;ReleaseStringUTFChars(env, name, str);
}</pre>
<p>To convert a C-style string to a jstring , you can use the (*env)-&gt;NewStringUTF() function to create a new jstring from a C-style string. For example, a C function that needs to return a Java string could contain the following code:</p>
<pre lang="c" line="1" escaped="true">JNIEXPORT jstring JNICALLJava_MyJavaClass_getName(JNIEnv *env, jobject obj)
{
	return (*env)-&gt;NewStringUTF(env, “My String”);
}</pre>
<p><strong>JNI String functions</strong>:</p>
<table class="tbl" border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="30%"><strong>JNI Function</strong></td>
<td width="70%"><strong>Description</strong></td>
</tr>
<tr>
<td>GetStringChars<br />
ReleaseStringChars</td>
<td>Obtains or releases a pointer to the contents of a string in Unicode format. May return a copy of the string.</td>
</tr>
<tr>
<td>GetStringUTFChars<br />
ReleaseStringUTFChars</td>
<td>Obtains or releases a pointer to the contents of a string in UTF-8 format.</td>
</tr>
<tr>
<td>GetStringLength</td>
<td>Returns the number of Unicode characters in the string.</td>
</tr>
<tr>
<td>GetStringUTFLength</td>
<td>Returns the number of bytes needed to represent a string in the UTF-8 format.</td>
</tr>
<tr>
<td>NewString</td>
<td>Creates a java.lang.String instance that contains the same sequence of characters as the given Unicode C string.</td>
</tr>
<tr>
<td>NewStringUTF</td>
<td>Creates a java.lang.String instance that contains the same sequence of characters as the given UTF-8 encoded C string.</td>
</tr>
<tr>
<td>GetStringCritical<br />
ReleaseStringCritical</td>
<td>Obtains a pointer to the contents of a string in Unicode format. May return a copy of the string. Native code must not block between a pair of Get/ReleaseStringCritical calls.</td>
</tr>
<tr>
<td>GetStringRegion<br />
SetStringRegion</td>
<td>Copies the contents of a string to or from a preallocated C buffer in the Unicode format.</td>
</tr>
<tr>
<td>GetStringUTFRegion<br />
SetStringUTFRegion</td>
<td>Copies the content of a string to or from a preallocated C buffer in the UTF-8 format.</td>
</tr>
</tbody>
</table>
<p><strong>JNI String Example:</strong></p>
<p>Let’s write a JNI application that passes a prompt message to a native method, which method prints the prompt message, reads the user input and sends it back to the application.</p>
<p>The Java code will be(NativePrompt.java):</p>
<pre lang="java" line="1" escaped="true">class NativePrompt {
	private native String getInput(String prompt);  //native method
	static   //static initializer code
	{
		System.loadLibrary("NativePrompt");
	} 

	public static void main(String[] args)
	{
		NativePrompt NP = new NativePrompt();
		String sName = NP.getInput("Enter your name: ");
        System.out.println("Hello " + sName);
	}
}</pre>
<p>Compile NativePrompt.java (javac NativePrompt.java) and generate hedder file(javah -jni NativePrompt).</p>
<p>The generated with javah header file NativePrompt.h will be:</p>
<pre lang="c" line="1" escaped="true">/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class NativePrompt */

#ifndef _Included_NativePrompt
#define _Included_NativePrompt
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif
</pre>
<p>The C++ implementation file NativePrompt.cpp will be:</p>
<pre lang="cpp" line="1" escaped="true">#include "NativePrompt.h" 
#include "jni.h"
#include "string"
#include "iostream"
#include "vector"

using namespace std;
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
	(JNIEnv *env, jobject obj, jstring prompt){
	
	string sEntry;
	const char *str;
	str = env-&gt;GetStringUTFChars(prompt, NULL);
	if (str == NULL) {
		return env-&gt;NewStringUTF("");
	}
	else{
	cout &lt;&lt; str; //Frees native string resources env-&gt;ReleaseStringUTFChars(prompt, str);
		
		//reads n-consecutive words from the 
		//keyboard and store them in string
		getline(cin, sEntry);
		
        return env-&gt;NewStringUTF(sEntry.c_str());
	}
}</pre>
<p>Run the program (java NativePrompt) and verify the output.</p><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/">JNI Part 4: JNI Strings</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-4-jni-strings/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>JNI Part 3: Passing Arguments and Mapping Types</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-3-mapping-types-passing-arguments/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-3-mapping-types-passing-arguments/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Mon, 22 Aug 2011 04:42:39 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[object types]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[primitive]]></category>
		<category><![CDATA[mapping types]]></category>
		<category><![CDATA[jni]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1664</guid>

					<description><![CDATA[<p>The implemented JNI native methods have two standard parameters, in addition to the arguments declared in their Java-side declaration. The first parameter, the JNIEnv interface pointer, points to a location that contains a pointer to a function table. Each entry in the function table points to a JNI function. Native methods always access data structures</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-3-mapping-types-passing-arguments/">JNI Part 3: Passing Arguments and Mapping Types</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<ol>
<li>The implemented JNI native methods have two standard parameters, in addition to the arguments declared in their Java-side declaration.</li>
<li>The first parameter, the JNIEnv interface pointer, points to a location that contains a pointer to a function table.</li>
<li>Each entry in the function table points to a JNI function.</li>
<li>Native methods always access data structures in the Java virtual machine through one of the JNI functions.</li>
<li>The second parameter differs depending on whether the native method is a static or an instance method.</li>
<li>The second argument to an instance native method is a reference to the object on which the method is invoked, similar to the ‘this’ pointer in C++.</li>
<li>The second argument to a static native method is a reference to the class in which the method is defined.</li>
<li>In the previous example, Java_HelloWorld_print() implements an instance native method. Thus the jobject parameter is a reference to the object itself.</li>
</ol>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-1666 size-full" title="JNI Passing arguments" src="https://studentprojects.in/wp-content/uploads/2011/08/JNI-Passing-arguments.png" alt="JNI Passing arguments" width="618" height="254" /></p>
<p><strong>Mapping Types</strong></p>
<ol>
<li>Argument types in the native method declaration have corresponding types in native programming languages.</li>
<li>The JNI defines a set of C and C++ types that correspond to types in the Java programming language.</li>
<li>There are two kinds of types in the Java programming language:
<ol>
<li>Primitive types such as int, float, double and char;</li>
<li>Reference types such as classes and arrays.</li>
</ol>
</li>
<li>The variables of primitive types contain a single value – a number, character or boolean value. A variable of primitive type has a specified size and format.</li>
<li>The variables of reference type contain a reference to (an address of) the value or set of values represented by the variable. Arrays, classes and interfaces are reference types.</li>
<li>The JNI treats primitive types and reference types differently.</li>
<li>The JNI passes objects to native methods as opaque references.</li>
<li>Opaque references are C pointer types that refer to internal data structures in the Java virtual machine.</li>
<li>The exact layout of the internal data structures, however, is hidden from the programmer.</li>
<li>All JNI references are of or inherit the type jobject.</li>
</ol>
<p><strong>Primitive types:</strong> <strong>There are eight primitive types in Java</strong></p>
<table class="tbl" border="0" width="450" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="200"><strong>Type</strong></td>
<td width="250"><strong>Description</strong></td>
</tr>
<tr>
<td>boolean</td>
<td>Similar to the C++ bool type but cannot be converted to int. An integer or pointer cannot be used in a boolean context (such as an if condition they way it can in C or C++.</td>
</tr>
<tr>
<td>char</td>
<td>Similar to the C char type but uses 16 bits.</td>
</tr>
<tr>
<td>byte</td>
<td>An 8-bit signed integer.</td>
</tr>
<tr>
<td>short</td>
<td>A 16-bit signed integer.</td>
</tr>
<tr>
<td>int</td>
<td>A 32-bit signed integer.</td>
</tr>
<tr>
<td>long</td>
<td>A 64-bit signed integer.</td>
</tr>
<tr>
<td>float</td>
<td>A 32-bit floating point number.</td>
</tr>
<tr>
<td>double</td>
<td>A 64-bit floating point number.</td>
</tr>
</tbody>
</table>
<p>The mapping of primitive types is straightforward.</p>
<p><strong>Example: </strong></p>
<ol>
<li>The type int in the Java programming language maps to the C/C++ type jint (defined in jni.h as a signed 32-bit integer),</li>
<li>The type float in the Java programming language maps to the C and C++ type jfloat (defined in jni.h as a 32-bit floating point number).</li>
</ol>
<table class="tbl" border="0" width="500" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="150"><strong>Java Type</strong></td>
<td width="150"><strong>Native Type</strong></td>
<td width="200"><strong>Size in bits</strong></td>
</tr>
<tr>
<td>boolean</td>
<td>jboolean</td>
<td>8, unsigned</td>
</tr>
<tr>
<td>byte</td>
<td>Jbyte</td>
<td style="text-align: left;">8</td>
</tr>
<tr>
<td>char</td>
<td>Jchar</td>
<td>16, unsigned</td>
</tr>
<tr>
<td>short</td>
<td>Jshort</td>
<td style="text-align: left;">16</td>
</tr>
<tr>
<td>int</td>
<td>Jint</td>
<td style="text-align: left;">32</td>
</tr>
<tr>
<td>long</td>
<td>Jlong</td>
<td style="text-align: left;">64</td>
</tr>
<tr>
<td>float</td>
<td>Jfloat</td>
<td style="text-align: left;">32</td>
</tr>
<tr>
<td>double</td>
<td>Jdouble</td>
<td style="text-align: left;">64</td>
</tr>
<tr>
<td>void</td>
<td>Void</td>
<td>n/a</td>
</tr>
</tbody>
</table>
<p><strong>Reference types:</strong></p>
<ol>
<li>Here Objects are passed by reference</li>
<li>All objects have type jobject as shown in below image</li>
</ol>
<p><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-1671" title="JNI Object mapping" src="https://studentprojects.in/wp-content/uploads/2011/08/JNI-Objectmapping.png" alt="" width="612" height="332" /></p>
<p>The native code must manipulate the underlying objects via the appropriate JNI functions, which are available through the JNIEnv interface pointer.</p>
<p><strong>Example:<br />
</strong>The corresponding JNI type for java.lang.String is jstring. The exact value of a jstring reference is irrelevant to the native code. The native code calls JNI functions such as GetStringUTFChars() to access the contents of a string.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">
<p><!-- [if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML /> <o:AllowPNG /> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves /> <w:TrackFormatting /> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF /> <w:LidThemeOther>EN-IN</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:SplitPgBreakAndParaMark /> <w:DontVertAlignCellWithSp /> <w:DontBreakConstrainedForcedTables /> <w:DontVertAlignInTxbx /> <w:Word11KerningPairs /> <w:CachedColBalance /> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math" /> <m:brkBin m:val="before" /> <m:brkBinSub m:val="&#45;-" /> <m:smallFrac m:val="off" /> <m:dispDef /> <m:lMargin m:val="0" /> <m:rMargin m:val="0" /> <m:defJc m:val="centerGroup" /> <m:wrapIndent m:val="1440" /> <m:intLim m:val="subSup" /> <m:naryLim m:val="undOvr" /> </m:mathPr></w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true" DefSemiHidden="true" DefQFormat="false" DefPriority="99" LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Normal" /> <w:LsdException Locked="false" Priority="9" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="heading 1" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9" /> <w:LsdException Locked="false" Priority="39" Name="toc 1" /> <w:LsdException Locked="false" Priority="39" Name="toc 2" /> <w:LsdException Locked="false" Priority="39" Name="toc 3" /> <w:LsdException Locked="false" Priority="39" Name="toc 4" /> <w:LsdException Locked="false" Priority="39" Name="toc 5" /> <w:LsdException Locked="false" Priority="39" Name="toc 6" /> <w:LsdException Locked="false" Priority="39" Name="toc 7" /> <w:LsdException Locked="false" Priority="39" Name="toc 8" /> <w:LsdException Locked="false" Priority="39" Name="toc 9" /> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption" /> <w:LsdException Locked="false" Priority="10" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Title" /> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font" /> <w:LsdException Locked="false" Priority="11" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtitle" /> <w:LsdException Locked="false" Priority="22" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Strong" /> <w:LsdException Locked="false" Priority="20" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Emphasis" /> <w:LsdException Locked="false" Priority="59" SemiHidden="false" UnhideWhenUsed="false" Name="Table Grid" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text" /> <w:LsdException Locked="false" Priority="1" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="No Spacing" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 1" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 1" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 1" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 1" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision" /> <w:LsdException Locked="false" Priority="34" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="List Paragraph" /> <w:LsdException Locked="false" Priority="29" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Quote" /> <w:LsdException Locked="false" Priority="30" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Quote" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 1" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 1" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 1" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 1" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 1" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 2" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 2" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 2" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 2" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 2" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 2" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 2" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 2" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 3" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 3" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 3" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 3" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 3" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 3" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 3" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 3" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 3" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 4" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 4" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 4" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 4" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 4" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 4" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 4" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 4" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 4" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 5" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 5" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 5" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 5" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 5" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 5" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 5" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 5" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 5" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 6" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 6" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 6" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 6" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 6" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 6" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 6" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 6" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 6" /> <w:LsdException Locked="false" Priority="19" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis" /> <w:LsdException Locked="false" Priority="21" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis" /> <w:LsdException Locked="false" Priority="31" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference" /> <w:LsdException Locked="false" Priority="32" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Reference" /> <w:LsdException Locked="false" Priority="33" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Book Title" /> <w:LsdException Locked="false" Priority="37" Name="Bibliography" /> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading" /> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} --> <!--[endif] --></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">Mapping Types</span></strong></p>
<p class="MsoNormal" style="margin-left: 36.0pt; text-indent: -18.0pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">Argument types in the native method deMapping Types<br />
?    Argument types in the native method declaration have corresponding types in native programming languages.<br />
?    The JNI defines a set of C and C++ types that correspond to types in the Java programming language.<br />
?    There are two kinds of types in the Java programming language:<br />
?    primitive types such as int, float, double and char;<br />
?    reference types such as classes and arrays.<br />
?    The variables of primitive types contain a single value – a number, character or boolean value. A variable of primitive type has a specified size and format.<br />
?    The variables of reference type contain a reference to (an address of) the value or set of values represented by the variable. Arrays, classes and interfaces are reference types.<br />
There are eight primitive types in Java:<br />
claration have <strong>corresponding types</strong> in native programming languages. </span></p>
<p class="MsoNormal" style="margin-left: 36.0pt; text-indent: -18.0pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">The JNI <strong>defines a set of C and C++ types</strong> that correspond to types in the Java programming language.</span></p>
<p class="MsoNormal" style="margin-left: 36.0pt; text-indent: -18.0pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">There are two kinds of types in the Java programming language:</span></p>
<p class="MsoNormal" style="margin-left: 72.0pt; text-indent: -18.0pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><strong><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">primitive types</span></strong><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">such as <strong>int, float</strong>, <strong>double</strong> and <strong>char</strong>;</span></p>
<p class="MsoNormal" style="margin-left: 72.0pt; text-indent: -18.0pt; mso-list: l0 level2 lfo1; tab-stops: list 72.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><strong><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">reference types</span></strong><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">such as <strong>classes</strong> and <strong>arrays</strong>. </span></p>
<p class="MsoNormal" style="margin-left: 36.0pt; text-indent: -18.0pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">The variables of primitive types <strong>contain a single value</strong> – a number, character or boolean value. A variable of primitive type has a <strong>specified size and format</strong>. </span></p>
<p class="MsoNormal" style="margin-left: 36.0pt; text-indent: -18.0pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt;"><span style="font-size: 14.0pt; line-height: 115%; font-family: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings;"><span style="mso-list: Ignore;">§</span></span><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">The variables of reference type <strong>contain a reference to</strong> (an address of) the value or set of values represented by the variable. Arrays, classes and interfaces are reference types.</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;"><span lang="EN-US" style="font-size: 14.0pt; line-height: 115%; mso-ansi-language: EN-US;">There are eight primitive types in Java:</span></strong></p>
</div><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-3-mapping-types-passing-arguments/">JNI Part 3: Passing Arguments and Mapping Types</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-3-mapping-types-passing-arguments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to convert a jstring to a C-style string or vice versa?</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Sat, 20 Aug 2011 12:57:56 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[NewStringUTF]]></category>
		<category><![CDATA[env]]></category>
		<category><![CDATA[GetStringUTFChars]]></category>
		<category><![CDATA[string to jstring]]></category>
		<category><![CDATA[jstring to string]]></category>
		<category><![CDATA[jni]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1674</guid>

					<description><![CDATA[<p>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *).&#160; So we cannot use a jstring as a normal C string. We must use the appropriate JNI functions to convert jstring objects to C/C++ strings.&#160; The JNI supports conversion both</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/">How to convert a jstring to a C-style string or vice versa?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>The jstring type represents strings in the Java virtual machine, and is different from the regular C string type (a pointer to characters, char *).&nbsp; So we cannot use a jstring as a normal C string. We must use the appropriate JNI functions to convert jstring objects to C/C++ strings.&nbsp; The JNI supports conversion both to and from Unicode and UTF-8 strings. Unicode strings represent characters as 16-bit values, whereas UTF-8 strings use an encoding scheme that is upward compatible with 7-bit ASCII strings. UTF-8 strings act like NULL-terminated C strings.</p>
<p>jstring, which requires a subroutine call to in order to convert a Java Unicode string (2 bytes) to a C-style char* string (1 byte UTF-8 format).</p>
<p>To convert a<strong> jstring to a C-style string</strong>, you might write code like the following:</p>
<pre lang="c" escaped="true" line="1">JNIEXPORT void JNICALLJava_MyJavaClass_printName(JNIEnv *env, jobject obj,
	jstring name)
{
	const char *str= (*env)-&gt;GetStringUTFChars(env,name,0);
	printf(“%s”, str);
	//need to release this string when done with it in order to
	//avoid memory leak
	(*env)-&gt;ReleaseStringUTFChars(env, name, str);
}</pre>
<p>To convert a<strong> </strong><strong>C-style string to </strong><strong>jstring </strong>, you can use the (*env)-&gt;NewStringUTF() function to create a new jstring from a C-style string. For example, a C function that needs to return a Java string could contain the following code:</p>
<pre lang="c" escaped="true" line="1">JNIEXPORT jstring JNICALLJava_MyJavaClass_getName(JNIEnv *env, jobject obj)
{
	return (*env)-&gt;NewStringUTF(env, “Electrofriends.com”);
}</pre><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/">How to convert a jstring to a C-style string or vice versa?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/convert-jstring-cstyle-string-vice-versa/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JNI Part 2: Visual Studio setup for DLL Project</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/part-2-jni-visual-studio-setup-dll-project/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/part-2-jni-visual-studio-setup-dll-project/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 11 Aug 2011 06:23:38 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1522</guid>

					<description><![CDATA[<p>Here is the simple steps to DLL project in Visual Studio Open Visual Studio Click File-&#62;New Project, and select Empty project Write Name and select location Click ok Go to Project-&#62;Properties On the left side, select general in configuration properties Select Dynamic Library(.dll) as configuration Type Next click C/C++ Add jdk include and win32 path</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/part-2-jni-visual-studio-setup-dll-project/">JNI Part 2: Visual Studio setup for DLL Project</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Here is the simple steps to DLL project in Visual Studio</p>
<ol>
<li>Open Visual Studio</li>
<li>Click File-&gt;New Project, and select Empty project</li>
<li>Write Name and select location</li>
<li>Click ok</li>
</ol>
<figure id="attachment_1528" aria-describedby="caption-attachment-1528" style="width: 603px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-1528" title="JNI: Creating new Dll project" src="https://studentprojects.in/wp-content/uploads/2011/07/CreateNewDllProject.png" alt="JNI: Creating new Dll project" width="603" height="429" /><figcaption id="caption-attachment-1528" class="wp-caption-text">JNI: Creating new Dll project</figcaption></figure>
<ol>
<li>Go to Project-&gt;Properties</li>
<li>On the left side, select general in configuration properties</li>
<li>Select Dynamic Library(.dll) as configuration Type</li>
</ol>
<figure id="attachment_1529" aria-describedby="caption-attachment-1529" style="width: 602px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-1529" title="Dll Project Configuration" src="https://studentprojects.in/wp-content/uploads/2011/07/DllProjectConfiguration.png" alt="Dll Project Configuration" width="602" height="421" /><figcaption id="caption-attachment-1529" class="wp-caption-text">Dll Project Configuration</figcaption></figure>
<ol>
<li>Next click C/C++</li>
<li>Add jdk include and win32 path in Additional Include Directories</li>
<li>If you have installed jdk in c program files then the path is</li>
<li>C:\Program Files\Java\jdk1.6.0_18\include; C:\Program Files\Java\jdk1.6.0_18\include\win32</li>
<li>Click OK button.</li>
</ol>
<figure id="attachment_1530" aria-describedby="caption-attachment-1530" style="width: 600px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-1530" title="Dll Project Add Include Directories" src="https://studentprojects.in/wp-content/uploads/2011/07/DllProjectAddIncludeDirectories.png" alt="Dll Project Add Include Directories" width="600" height="419" /><figcaption id="caption-attachment-1530" class="wp-caption-text">Dll Project adding include directories</figcaption></figure>
<ol>
<li>Now the configuration of the project is done. Next step is to add files to the project.</li>
<li>As shown in below image, right click on the “Header Files” folder and select Add, Existing Item</li>
<li>Select Header files and add to the project.</li>
</ol>
<figure id="attachment_1531" aria-describedby="caption-attachment-1531" style="width: 545px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-1531" title="DLL adding files" src="https://studentprojects.in/wp-content/uploads/2011/07/DLL-adding-files.png" alt="DLL adding files" width="545" height="375" /><figcaption id="caption-attachment-1531" class="wp-caption-text">DLL adding files</figcaption></figure>
<p>Finally solution explorer will look like:</p>
<figure id="attachment_1532" aria-describedby="caption-attachment-1532" style="width: 221px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-1532" title="Solution Explorer" src="https://studentprojects.in/wp-content/uploads/2011/07/SolutionExplorer.png" alt="Solution Explorer after adding files" width="221" height="285" /><figcaption id="caption-attachment-1532" class="wp-caption-text">Solution Explorer</figcaption></figure>
<ol>
<li><strong>Build Solution: </strong>Go to Build-&gt;Build Solution</li>
<li>Solve the errors if any found in output window.</li>
<li>Once the build is successful then the generated dll will be in the output directory.</li>
</ol><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/part-2-jni-visual-studio-setup-dll-project/">JNI Part 2: Visual Studio setup for DLL Project</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/part-2-jni-visual-studio-setup-dll-project/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>JNI Part1: Java Native Interface Introduction and &#8220;Hello World&#8221; application</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part1-java-native-interface/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part1-java-native-interface/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Fri, 15 Jul 2011 13:33:39 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1508</guid>

					<description><![CDATA[<p>Introduction, Purpose and features JNI stands for Java Native Interface JNI specifies a communication protocol between Java code and external, native code. It enables your Java code to interface with native code written in other languages (such as C, C++) Native code typically accesses the CPU and registers directly and is thus faster than interpreted</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part1-java-native-interface/">JNI Part1: Java Native Interface Introduction and “Hello World” application</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction, Purpose and features</strong></p>
<ol>
<li>JNI stands for <strong>Java Native Interface</strong></li>
<li>JNI specifies a communication protocol between Java code and external, native code.</li>
<li>It enables your Java code to interface with native code written in other languages (such as C, C++)</li>
<li>Native code typically accesses the CPU and registers directly and is thus faster than interpreted code (like Java)</li>
<li>Java native methods are methods declared in your Java code (much like you declare an abstract method), but which are actually implemented in another programming language.</li>
</ol>
<figure style="width: 782px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="JNI - Java Native Interface" title="JNI - Java Native Interface" src="https://studentprojects.in/wp-content/uploads/2011/07/JNI-Java-Native-Interface.jpg" alt="JNI - Java Native Interface" width="782" height="327" /><figcaption class="wp-caption-text">JNI &#8211; Java Native Interface</figcaption></figure>
<p><strong>JNI allows Java programmers to</strong></p>
<ol>
<li>Leverage platform specific features outside a JVM. For example, you typically can&#8217;t write a device driver in Java because you can&#8217;t directly access the hardware</li>
<li>Leverage the improved speed possible with natively compiled code (such as C or assembler). For example, you might need an extremely fast math routine for a scientific application or game program.</li>
<li>Utilize existing code libraries in your Java programs. For example, there might be a really good file compression library written in C. Why try to rewrite it in Java when you can access it using JNI?</li>
</ol>
<p><strong>JNI Drawbacks</strong></p>
<ol>
<li>Your program is no longer platform independent</li>
<li>Your program is not as robust. If there is a null pointer exception in your native code, the JVM can&#8217;t display a helpful message. It might even lock up.</li>
</ol>
<p><strong>JNI supports</strong></p>
<ol>
<li>Native methods can create and manipulate Java objects such as strings and arrays.</li>
<li>Native methods can manipulate and update Java objects passed into them (as parameters)</li>
<li>You can catch and throw exceptions from native methods and handle these exceptions in either the native method or your Java application</li>
<li>This almost seamless sharing of objects makes it very easy to incorporate native methods in your Java code</li>
</ol>
<p><strong>The Role of JNI</strong></p>
<ol>
<li>As a part of the Java virtual machine implementation, the JNI is a two-way interface that allows Java applications to invoke native code and vice versa.</li>
<li>The JNI is designed to handle situations where you need to combine Java applications with native code.</li>
<li>As a two-way interface, the JNI can support two types of native code: native libraries and native applications.</li>
</ol>
<p style="text-align: center;"><img decoding="async" loading="lazy" class="size-full wp-image-1510 aligncenter" title="Role of JNI" src="https://studentprojects.in/wp-content/uploads/2011/07/Roll_of_JNI.png" alt="" width="600" height="233" /></p>
<h2>JNI “Hello World!” Application</h2>
<p style="text-align: justify;"><strong>Let’s create the JNI “Hello World” application</strong> – a Java application that calls a C function via JNI to print “Hello World!”.</p>
<p style="text-align: justify;">The process consists of the following steps:</p>
<ol>
<li>Create a class (HelloWorld.java) that declares the native method.</li>
<li>Use javac to compile the HelloWorld source file, resulting in the class file HelloWorld.class.</li>
<li>Use javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation. The javah tool is provided with JDK or Java 2 SDK releases.</li>
<li>Write the C implementation (CLibHelloWorld.c) of the native method.</li>
<li>Compile the C implementation into a native library, creating HelloWorld.dll.</li>
<li>Run the HelloWorld program using the java runtime interpreter. Both the class file (HelloWorld.class) and the native library (HelloWorld.dll) are loaded at runtime.</li>
</ol>
<p><a href="https://studentprojects.in/wp-content/uploads/2011/07/JNI-Helloword-demo.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-1517" title="JNI Helloword demo" src="https://studentprojects.in/wp-content/uploads/2011/07/JNI-Helloword-demo.png" alt="" width="426" height="644" /></a>The program defines a class named HelloWorld that contains a native method print().<br />
1. Create a class (HelloWorld.java)</p>
<pre lang="java">class HelloWorld {
	public native void print();  //native method
	static   //static initializer code
	{
		System.loadLibrary("CLibHelloWorld");
	} 

	public static void main(String[] args)
	{
		HelloWorld hw = new HelloWorld();
        hw.print();
	}
}</pre>
<p>There are two remarkable things here.</p>
<p>1. First one is the declaration of a native method using the keyword <span style="color: #ff0000;">native</span>.</p>
<pre lang="java">public native void print();</pre>
<p>This tells the compiler that print() will be accessed from an external library, and that it should not look for it in the Java source code. Accordingly, notice that there is no implementation of this method present in the Java code.</p>
<p>2. The second remarkable section of code is the following:</p>
<pre lang="java">static {
        System.loadLibrary(“CLibHelloWorld"); //**** Loads CLibHelloWorld.dll
}</pre>
<p>•    Before the native method print() can be called, the native library that implements print must be loaded. The code above loads the native library in the static  initializer of the HelloWorld class.<br />
•    The Java VM automatically runs the static initializer before invoking any methods in the CLibHelloWorld class.</p>
<p>Next we compile the JNI HelloWorld java application as follows:</p>
<p>Having the application compiled we can use the javah tool to generate the header (h) file that declares the native method print().</p>
<pre lang="java">javah –jni HelloWorld</pre>
<ul>
<li>&#8211; The name of the header file is the class name with a “.h” appended to the end of it.</li>
<li>&#8211; The command shown above generates a file named HelloWorld.h.</li>
</ul>
<pre lang="c">/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif</pre>
<p>In the below line</p>
<pre lang="c">JNIEXPORT void JNICALL Java_HelloWorld_print  (JNIEnv *, jobject);</pre>
<p>•    The method name is prefixed with Java_ and then the full name of the class to which it belongs, then the function name.<br />
•    This is to distinguish it from methods that belong to other classes and might have the same name.<br />
•    The first argument for every native method implementation is a JNIEnv interface pointer.<br />
•    The second argument is a reference to the HelloWorld object itself.</p>
<p><strong>Writing the implementation:</strong></p>
<p>Write the native C/C++ code which implements the method. Use the same signature that your header file uses. You might name your file something like &#8220;CLibHelloWorld.c&#8221;.</p>
<pre lang="c">#include "HelloWorld.h"
#include "jni.h"
#include  "stdio.h"

JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
  printf("Hello world\n");
  return;
}</pre>
<p>Here</p>
<ul>
<li>We use the <strong>printf</strong> function to display the string “Hello World!”.</li>
<li>Both arguments, the <strong>JNIEnv</strong> pointer and the reference to the object, are ignored.</li>
<li>The C program includes three header files:
<ul>
<li>jni.h &#8211; provides information the native code needs to call JNI functions.</li>
<li>stdio.h – implements the printf() function.</li>
<li>HelloWorld.h – generated by using javah, it includes the C/C++ prototype for the Java_HelloWorld_print function.</li>
</ul>
</li>
</ul>
<p>Compile the C/C++ code into a library (a DLL if your code will run under Windows). &#8211; <a href="https://studentprojects.in/articles/jni/part-2-jni-visual-studio-setup-dll-project/" target="_blank" rel="noopener">Click here to read step by step procedure to create DLL project using Visula Studio.</a></p>
<p>If you use C++ Builder to compile your library under Windows, make sure you create a DLL project and then add the C/C++ file to it (e.g. CLibHelloWorld.c). You&#8217;ll need to add the following to your compiler&#8217;s include path:</p>
<ul>
<li>\\javadir\\include</li>
<li>\\javadir\\include\\win32</li>
</ul>
<p>Be sure to name your project CLibHelloWorld so the DLL it creates will be named c_library.dll.</p>
<p>Run the Java class (main method). Be sure all the files and dll are in one folder.</p>
<pre lang="java">java HelloWorld</pre>
<p>You should see &#8220;Hello world&#8221; appear on the screen!</p>
<p>If you see a &#8220;java.lang.UnsatisfiedLinkError&#8221; error message, then Java couldn&#8217;t find your shared library OR mismatch in native functions.</p><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part1-java-native-interface/">JNI Part1: Java Native Interface Introduction and “Hello World” application</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part1-java-native-interface/feed/</wfw:commentRss>
			<slash:comments>15</slash:comments>
		
		
			</item>
	</channel>
</rss>
