System类包含一些有用的类字段和方法。它不能被实例化。
方法:
1、public static void gc():运行垃圾回收器。
2、public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
3、public static long currentTimeMillis():返回以毫秒为单位的当前时间
4、public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
1、
1 public static void gc():运行垃圾回收器。 2 public class SystemDemo { 3 public static void main(String[] args) { 4 Person p = new Person("阿三", 34); 5 System.out.println(p); 6 7 p = null; // 让p不再指定堆内存 8 System.gc(); //直接调用就行了,不过很少用这个,因为占用资源太多,平时jvm默认的清理就行了 9 } 10 }
2、
public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
public static long currentTimeMillis():返回以毫秒为单位的当前时间
1 public class SystemDemo { 2 public static void main(String[] args) { 3 // System.out.println("我们喜欢林青霞(东方不败)"); 4 // System.exit(0); 5 // System.out.println("我们也喜欢赵雅芝(白娘子)"); 6 7 // System.out.println(System.currentTimeMillis()); 8 9 // 单独得到这样的实际目前对我们来说意义不大 10 // 那么,它到底有什么作用呢? 11 // 要求:请大家给我统计这段程序的运行时间 12 long start = System.currentTimeMillis(); 13 for (int x = 0; x < 100000; x++) { 14 System.out.println("hello" + x); 15 } 16 long end = System.currentTimeMillis(); 17 System.out.println("共耗时:" + (end - start) + "毫秒"); 18 } 19 }
3、public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
1 public class SystemDemo { 2 public static void main(String[] args) { 3 // 定义数组 4 int[] arr = { 11, 22, 33, 44, 55 }; 5 int[] arr2 = { 6, 7, 8, 9, 10 }; 6 7 // 请大家看这个代码的意思 8 System.arraycopy(arr, 1, arr2, 2, 2); 9 10 System.out.println(Arrays.toString(arr)); //[11, 22, 33, 44, 55] 11 System.out.println(Arrays.toString(arr2)); //[6, 7, 22, 33, 10] 12 } 13 }