System类
概述:
在System类提供的设施中,有标准输入、标准输出和错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。
成员方法:
public static void gc() //运行垃圾回收器
public static void exit(int status) //终止当前正在执行虚拟机
public static long currentTimeMillis() //返回以毫秒为单位的当前时间,1970年1月1日到现在的时间
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代码(gc()):
public class Demo3_System {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
new Demo();
System.gc();
}
}
}
class Demo{
private static int id = 1;
@Override
public void finalize() {
System.out.println("垃圾被回收了" + id++);
}
}
输出:
垃圾被回收了1
垃圾被回收了2
垃圾被回收了3
垃圾被回收了4
垃圾被回收了5
垃圾被回收了6
垃圾被回收了7
垃圾被回收了8
垃圾被回收了9
垃圾被回收了10
代码(arrayCopy()):
import java.util.Arrays;
public class Demo4_System {
public static void main(String[] args) {
int[] src = {0, 1, 2, 3, 4, 5, 6, 7};
int[] dest = new int[8];
System.arraycopy(src, 0, dest, 2, 6);
System.out.println(Arrays.toString(dest));
}
}
输出:
[0, 0, 0, 1, 2, 3, 4, 5]