Java Native Method[»¹Ã»À´µÃ¼°·Òë]
The goal for this chapter is to introduce you to Java's native methods. If you are new to Java, you may not know what native methods are, and even if you are an experienced Java developer, you may not have had a reason to learn more about native methods. At the conclusion of this chapter you should have a better understanding of what native methods are, when and why you may want to use them, and the consequences of using them. You should also have a basic understanding of how native methods work. You will then be more than ready to tackle the next three chapters, which dive into the nitty-gritty details of Java's Native Methods.
What Is a Native Method?
Simply put, 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 likely C (see Figure 30.1). This feature is not special to Java. 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. It is common to see the qualifier pascal in many C compilers to signal that the calling convention should be done in a Pascal convention, rather than a C convention. FORTRAN and Pascal have similar facilities, as do most other languages.
Figure 30.1 : A native method is a Java method whose implementation is provided by non-java code.
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 Nativ
Ïà¹ØÎĵµ£º
......
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
impo ......
±¾ÃÕÌâÀûÓÃÁËJava±à³ÌÓïÑÔÖÐÒ»¸öºÜÉÙ±»ÈËÁ˽âµÄÌØÐÔ¡£Ç뿼ÂÇÏÂÃæµÄ³ÌÐò½«»á×öЩʲô£¿
public class BrowserTest {
public static void main(String[] args) {
System.out.print("iexplore:");
http://www.google.com;
System.out.println(":maximize");
}
}
ÕâÊÇÒ»¸öÓеã¹îÒìµÄÎ ......
ÏÂÃæµÄ³ÌÐò¼ÆËãÁËÒ»¸öÑ»·µÄµü´ú´ÎÊý£¬²¢ÇÒÔÚ¸ÃÑ»·ÖÕֹʱ½«Õâ¸ö¼ÆÊýÖµ´òÓ¡Á˳öÀ´¡£ÄÇô£¬Ëü´òÓ¡µÄÊÇÊ²Ã´ÄØ£¿
public class InTheLoop {
public static final int END = Integer.MAX_VALUE;
public static final int START = END - 100;
public static void main(String[] args) {
int count = 0 ......
Java´úÂë
long startTime=System.currentTimeMillis(); //»ñÈ¡¿ªÊ¼Ê±¼ä
doSomeThing(); //²âÊԵĴúÂë¶Î
long endTime=System.currentTimeMillis(); //»ñÈ¡½áÊøÊ±¼ä
System.out.println("³ÌÐòÔËÐÐʱ¼ä£º "+(endTime-startTime)+"ms");
µÚ¶þÖÖÊÇÒÔÄÉÃëΪ ......