What Is A Native Method

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View What Is A Native Method as PDF for free.

More details

  • Words: 693
  • Pages: 9
What Is a Native Method? A native method is the Java interface to non-Java code . It is Java's link to the "outside world . More specifically, a native method is a Java method whose implementation is provided by non-Java code Most languages provide some mechanism to call routines written in another language. In C++, you must use the extern "C" stmt to signal that the C++ compiler is making a call to C functions In Java, this is done via native methods. In your Java class, you mark the methods you wish to implement outside of Java with the native method modifier-much like you would use the public or static modifiers

What Is a Native Method? In Java, this is done via native methods. In your Java class, you mark the methods you wish to implement outside of Java with the native method modifier-much like you would use the public or static modifiers  Then, rather than supplying the method's body, you simply place a semicolon in its place. As an example, the following class defines a variety of native methods public class IHaveNatives { native public void Native1( int x ) ; native static public long Native2() ; native synchronized private float Native3( Object o ) ; native void Native4( int[] ary ) throws Exception ; }

What Is a Native Method? native methods look much like any other Java method, except a single semicolon is in the place of the method body . Naturally, the body of the method is implemented outside of Java. What you basically define is the interface into this external method Every other Java method modifier can be used along with native, except abstract native methods can be static methods, thus not requiring the creation of an object (or instance of a class) native methods can limit their visibility with the public, private, private protected, protected, or unspecified default access. Native methods can also be synchronized

What Is a Native Method? There is no special procedure within the Java code to pass data to the native method. A native method can accept and return any of the Java types-including class types. the power of exception handling is also available to native methods When a native method receives complex types, such as class types (such as Object in the example) or array types (such as the int[] in the example), it has access to the contents of those types. However, the method used to access the contents may vary depending on the Java implementation being used. The presence of native methods does not affect how other classes call those methods.

What Is a Native Method? The caller does not even realize it is calling a native method, so no special code is generated, and the calling convention is the same as for any other method-the calling depends on the method being virtual or static The Java virtual machine will handle all the details to make the call in the native method implementation Native methods are very powerful, because they effectively extend the Java virtual machine. your Java code already uses native methods. In the current implementation from Sun, native methods are used in many places to interface to the underlying operating system

Uses for Native Methods Accessing Outside the Java Environment Accessing the Operating System Embedded Java Custom Hardware Sun's Java Performance Accessing Existing Libraries

Benefits of Native Methods Platform Neutrality Security Concerns System Safety Dependence on the Java Implementation

A Java Class with Native Methods simple look at a Java class containing native methods. This class encapsulates the

base and height of a triangle and contains a native method named ComputeArea that calculates the area of the triangle Triangle.java. public class Triangle { public void SetBase(float fInBase) { fBase = fInBase; } public void SetHeight(float fInHeight) { fHeight = fInHeight; } public native float ComputeArea(); // Load the native routines. static { System.loadLibrary("Triangle"); } float fBase; float fHeight; }

A Java Class with Native Methods

Compiling the Java Class C:\java\classes\Triangle> javac Triangle.java C:\java\classes\Triangle> Using javah to Generate the Header File C:\java\classes\Triangle> javah Triangle.java C:\java\classes\Triangle> Using javah-stubs to Generate a Stubs File C:\java\classes\Triangle> javah -stubs Triangle C:\java\classes\Triangle>

Related Documents

What Is A Native Method
November 2019 2
What Is A Patent
June 2020 7
What Is A Ads
November 2019 25
What Is A Geopolymer
November 2019 27
What Is A Lease
June 2020 14
What Is A Subnet
May 2020 10