Java.lang.System类中提供了大量的静态方法,可以获取系统相关的信息或进行系统级的操作。
currentTimeMillis方法
public static lang currentTimeMillis():返回以毫秒为单位的当前时间。
/**
* @description: System类方法测试
* @author: Murphy
* @date: 2020/6/309:58 上午
*/
public class SystemDemo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 500000; i++) {
int a = 0;
int b = a + i;
}
long end = System.currentTimeMillis();
//得到for循环的运算时间
System.out.println(end - start);
}
}
arraycopy方法
public static void arraycopy( Object src, int srcPos, Object dest, int destPos, int length):将数组中指定的数据拷贝到另一个数组中。
数组的拷贝动作是系统级的,性能很高,该方法有五个参数,含义如下:
参数 | 含义 |
---|---|
Object src | 源数组 |
int srcPos | 源数组索引起始位置 |
Object dest | 目标数组 |
int destPos | 目标数组索引起始位置 |
int length | 复制元素个数 |
/**
* @description: System类方法测试
* @author: Murphy
* @date: 2020/6/309:58 上午
*/
public class SystemDemo {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 6};
int[] b = {7, 8, 9, 10, 11, 12};
System.arraycopy(a, 0, b, 0, 3);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+" ");
}
}
}
//输出结果:1 2 3 10 11 12
exit方法
public static void exit(int status):终止当前正在运行的java虚拟机。
status状态码非零时表示异常终止。