Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

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


Ïà¹ØÎĵµ£º

JAVA ¾²Ì¬±äÁ¿Ó뾲̬·½·¨

javaÖо²Ì¬±äÁ¿ºÍ¾²Ì¬·½·¨·Ö±ðÓÐÊ²Ã´ÌØµã£¿
ÐüÉÍ·Ö£º0 - ½â¾öʱ¼ä£º2006-4-10 10:28

ÌáÎÊÕߣº vv_clear - ¶þ¼¶
×î¼Ñ´ð°¸
ΪʲôÎÊÁËÁ½´Î£¿ÔÙÌùÉÏÁíһƪ
Thinking:JavaÖÐstaticÓ÷¨- -
Tag£º Thinking:JavaÖÐs
Ò»¡¢static
ÇëÏÈ¿´ÏÂÃæÕâ¶Î³ÌÐò£º
public class ......

Java½â»ó2 21ÎÒµÄÀàÊÇʲôII

ÏÂÃæµÄ³ÌÐòËùÒª×öµÄÊÂÇéÕýÊÇǰһ¸öÃÕÌâËù×öµÄÊÂÇ飬µ«ÊÇËüûÓмÙÉèб¸Ü·ûºÅ¾ÍÊÇ·Ö¸ôÎļþÃû×é³É²¿·ÖµÄ·ûºÅ¡£Ïà·´£¬¸Ã³ÌÐòʹÓõÄÊÇjava.io.File.separator£¬Ëü±»Ö¸¶¨ÎªÒ»¸ö¹«¹²µÄStringÓò£¬°üº¬ÁËÆ½Ì¨Ïà¹ØµÄÎļþÃû·Ö¸ô·û¡£ÄÇô£¬Õâ¸ö³ÌÐò»á´òÓ¡³öÆäÕýÈ·µÄ¡¢Æ½Ì¨Ïà¹ØµÄÀàÎļþÃûÂð£¿
package com.javapuzzlers;
import java. ......

Java½â»ó2 23²»ÀÍÎÞ»ñ

ÏÂÃæµÄ³ÌÐò½«´òÓ¡Ò»¸öµ¥´Ê£¬ÆäµÚÒ»¸ö×ÖĸÊÇÓÉÒ»¸öËæ»úÊýÉú³ÉÆ÷À´Ñ¡ÔñµÄ¡£ÇëÃèÊö¸Ã³ÌÐòµÄÐÐΪ£º
import java.util.Random;
public class Rhymes {
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch(rnd.nextInt(2)) {
......

Java½â»ó2 25ÎÞÇéµÄÔöÁ¿²Ù×÷

ÏÂÃæµÄ³ÌÐò¶ÔÒ»¸ö±äÁ¿Öظ´µØ½øÐÐÔöÁ¿²Ù×÷£¬È»ºó´òÓ¡ËüµÄÖµ¡£ÄÇôÕâ¸öÖµÊÇÊ²Ã´ÄØ£¿
public class Increment {
public static void main(String[] args) {
int j = 0;
for (int i = 0; i < 100; i++)
j = j++;
System.out.println(j);
}
}
Õ§Ò»¿´£¬Õâ¸ö³ÌÐò¿ÉÄÜ» ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ