Basic knowledge of JNI language

Introduction to JNI

JNI is the abbreviation of Java Native Interface, It provides several APIs to realize the communication between Java and other languages (mainly C & C + +). Since Java 1.1, the JNI standard has become a part of the Java platform. It allows java code to interact with code written in other languages. JNI was originally designed for locally compiled languages, especially C and C + +, but it does not prevent you from using other programming languages, as long as the calling convention is supported. Use Java and locally compiled code Interaction usually loses platform portability. However, in some cases, this is acceptable or even necessary. For example, use some old libraries to interact with hardware, operating system, or to improve program performance. The JNI standard should at least ensure that the native code can work in any Java virtual machine environment.

1、 Parameters of the local method

How to declare a local method as static? The local method in the generated header file is as follows:

What do these two parameters mean: the jnienv type actually represents the Java environment. Through this jnienv * pointer, you can operate on the Java side code. For example, create objects of Java classes, call methods of Java objects, obtain properties of Java objects, and so on. Jnienv's pointer will be passed into the implementation function of the local method by JNI to operate the Java side code. Jclass represents the class object that calls the static method class, that is, the bytecode of the class.

If the local method is not declared as static, the form of the local method in the generated header file is as follows:

The jobobject type is equivalent to the object type in Java. It represents the object that calls the local method. In fact, to be precise, it maintains a reference to the object that calls the method. For example, if it is new nativetest() Sayhello () calls this method, and the reference points to the new nativetest () object.

There are many functions in the jnienv class that can be used:

Newobject creates an object

NewString creates a string object

New < type > array creates an array of a certain type

Get / set < type > field gets or sets the property of a member variable

Get / setstatic < type > field gets or sets the property of a static member variable

Call < type > method / callstatic < type > method calls the method and / or static method of a Java object

And so on, many functions

2、 Numerical parameters

When calling C \ C + + code with java code, there must be value passing. They belong to different programming languages and have many differences in data types. We should know their corresponding types. For example, although C has int and long data types, their implementation depends on the platform. On some platforms, the int type is 16 bit, while on others, it is a 32-bit integer. For this reason, the Java Native interface defines jint, jlong, and so on.

According to the correspondence between Java types and C / C + + data types, it can be seen that the newly defined type names are consistent with the Java type names, but a j is added in front, such as int corresponds to jint and long corresponds to jlong.

Let's see JNI H and JNI_ Md.h header file, you can more intuitively understand:

As you can see, for example, jint represents the same type as Java type int, but it is not int in C / C + +. As can be seen from the definition, int in Java, that is, jint in JNI, corresponds to long type in C / C + +.

Therefore, if you want to define a jint type data in the local method, the specification should be jint I = 10L;

For example, jchar represents a char type of Java type. In fact, it is an unsigned short type in C / C + +. In C / C + +, there is such a definition: typedef unsigned short wchar_ t。 Therefore, jchar is equivalent to wide characters in C / C + +, that is, char in Java is equivalent to wide characters in C / C + +. Therefore, if you want to define a jchar type data in the local method, the specification should be jchar C = l'c ';

In fact, all types with J represent types in Java. They are different from types in C / C + +, so they have to be converted internally. We also need to be careful when using them. You can see the following operations on strings in JNI.

Three. The form of Java objects in C \ C + + code

View JNI H header file, you can see the following contents

In fact, these are added_ J initial classes are integrated in_ Jobject, which is also to take care of our in Java. The object class is the base class of all classes.

These classes correspond to classes in Java, but they have changed their forms in C / C + +.

4、 Jclass class and how to get jclass object

In Java, class type represents the bytecode compiled by a class, that is, this class, which contains all the information of this class. In JNI, such a class is also defined, that is, jclass. People who know about reflection know how important class is. They can obtain Java class information and access its methods and member variables through reflection. Therefore, in JNI, there is also such a jclass class to represent the class class. Jnienv has several methods to get jclass objects:

Findclass will find the class under the system classpath and pass in the complete type. Note that the package interval uses /, not For example:

If you get the class object of the Java class, that is, jclass, you can use the form similar to reflection to get the member variables and methods in the Java class. Although the method is different from that of class, it is basically the same form.

summary

The above is the basic knowledge of JNI language introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>