定义:"A native method is a Java method whose implementation is provided by non-java code."
这种方法基本存在于基础类库,比如System类下的arraycopy方法声明如下:
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
这个方法用于数组的快速拷贝,源码中解释如下: a utility method for quickly copying a portion of an array.
测试代码如下:
/*
* 测试一下System的arraycopy方法
*/
import java.util.Arrays;
public class Test06{
public static void main(String[] args){
int[] a = {1,2,3,4,5};
int[] b = new int[a.length];
System.arraycopy(a,1,b,0,3);
String str = Arrays.toString(a);
System.out.println("数组a:" + str);
str = Arrays.toString(b);
System.out.println("数组b:" + str);//[2, 3, 4, 0, 0]
//测一下是深拷贝还是浅拷贝
a[2] = 2;
str = Arrays.toString(a);
System.out.println("数组a:" + str);
str = Arrays.toString(b);
System.out.println("数组b:" + str);//[2, 3, 4, 0, 0]
}
}