System

来源:互联网 发布:手机淘宝直通车在哪 编辑:程序博客网 时间:2024/06/16 23:46

System

System 类包含一些有用的类字段和方法。它不能被实例化。

 经常用在输出语句中 键盘录入中充当了标准输入流 InputStream(字节输入流) is = System.in;抽象类的多态! 

常用方法
1. public static void gc()运行垃圾回收器,其实是调用了Object中的finalize()方法,启动GC垃圾回收器

代码;
public static void main(String[] args) {

 person p = new person("张三",20); System.out.println(p); p=null; System.gc();

}
注意:需要重写toString()和finalize()方法

2.public static void exit(int status)终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。

3.public static long currentTimeMillis()返回以毫秒为单位的当前时间

代码
public static void main(String[] args) {
long start = System.currentTimeMillis();
for(int x=0;x<899;x++){
System.out.println(“hello”+x);
}
long end = System.currentTimeMillis();
System.out.println(“该循环所用时间为”+(end-start)+”秒”);

    System.exit(0);    System.out.println("结束了吗?");}

System类中和数组有关系的方法

    public static void arraycopy(Object src,int srcPos, Object dest,  int destPos, int length)从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束*代码*:    public static void main(String[] args) {    int [] arr1={1,3,4,5,6,7};    int [] arr2={2,34,56,7,8};    System.arraycopy(arr1,1,arr2,2,3);    System.out.println(Arrays.toString(arr1));    System.out.println(Arrays.toString(arr2));}
原创粉丝点击