Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Native keyword in Java

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the Native keyword in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Syntax of the Native keyword in Java

The syntax of native code is the same as that of a normal function definition, with the "native" keyword added at the beginning of the function.

For example:

Public class testing {public native String testMethod (String parameter);}

In this case, public is an access modifier. It should be public so that other files can use it. A string is the return data type of a function. It can be an integer, a character, or a Boolean, depending on the keyword. The argument passed to this function is also a data type string. Everything should be kept at the bottom of society.

After the function declaration, we call the function by creating an object and loading the library.

Public static void main (String [] args) {System.loadLibrary ("testing"); testing testingnew = new testing (); String output = testingnew.stringMethod ("NATIVE");}

You should first load the library defined above and create the object. With the help of this object, the native function is called.

2. How does the Native keyword work?

There should be two files. One contains the JAVA code, while the other should contain the legacy code of Candlestick +. The Java code will be used to invoke legacy code. This legacy code will interact with the hardware and return the expected output.

When legacy code interacts with hardware, it will not follow the guidelines laid down by JAVA. This code performs the necessary processing to get the output and pass the result to JNI. The Java native interface then checks into its directory, which contains all the rules related to the native code (this is located in SDK under a file named javah.exe). JNI is designed as part of the Java toolkit. After this processing, JAVA will publish the output in the JAVA language itself. When we create a JAVA program, we must make sure that there is a variable / data flow link between the JAVA file and the legacy file so that there is a smooth data flow between the two.

Here are the steps to explain how to use our keywords:

1. Write JAVA code that contains native methods, loaded shared libraries, and save it using "filename.JAVA".

two。 Compile the JAVA code and convert it to bytecode.

3. Create a Cmax Cobb + header file that contains the signature of the native function that should be called.

4. There is an implementation of a native method to write the code for C _.

5. Run the JAVA executable to see the results.

Third, code example

We write code in Eclipse and run the code to create a library, and then use that library to implement the C code.

Code:

Public class JPP {public static native void pAccess (); public static native int pRead (); public static native void pWrite (int port, int output); static {System.loadLibrary ("JPPlibs");} public void jAccess () {JPP.pAccess ();} public int jRead () {return JPP.pRead ();} public void jWrite (int port, int output) {JPP.pWrite (port, output);}}

After saving this code to the new "class" of the java project, we must set up a runtime environment to generate the header file.

When we run this program, we should generate a library.

Output:

Here we create a header file from the java code, which links the native code to the java language.

IV. Advantages of the Native keyword in Java

Here are some advantages:

It gives JAVA an additional advantage by interacting with code written in other languages and reducing the amount of work required to write the same code in JAVA, thereby reducing code redundancy.

It improves overall code performance. Because the code is written in other languages, it may be faster to use the machine language than to use JAVA. We can then call this code using the JAVA program.

Using this method, we can make system calls directly. Reduce the probability of external interference and improve the speed of code execution.

You can use any driver code written in JAVA to call preloaded libraries dynamically (written in any language other than JAVA), and you can still get a response in JAVA.

It enables JAVA to access hardware resources that can only be used by other languages.

If you have built a platform-dependent code for your application and its functionality is not supported by JAVA, we can use native code and link it to JAVA through the native keyword.

V. rules

The rules for the native keyword are as follows:

The native keyword will be used before the method name.

The native method declaration has no body and should end with a semicolon, because these methods are not defined in JAVA, but appear in the Cpassword + language.

Native methods cannot be declared as abstract methods.

Since there is no guarantee that the old code was written in accordance with the IEEE 754 standard (the IEEE floating-point algorithm standard was developed by the Institute of Electrical and Electronic Engineers in 1985), we cannot declare these native methods as strict FTP.

JAVA designs the JAVA native native interface (JNI) specification to define rules and declarations that implement native methods, such as data type conversions between JAVA and native code.

This is the end of the article on "how to use the Native keyword in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report