Java常用类(7)- System类

System类

System系统类,主要用于获取系统的熟悉数据和其他操作,构造方法私有

 

复制数组

System.arraycopy()

 

package commonclass.systemclass;

import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) {
        //arraycopy:数组的复制
        /*
        src:源数组
        srcPos:从哪个位置开始复制
        dest:目标数组
        destPos:目标数组的位置
        length:复制的长度
        System.arraycopy(src,srcPos,dest,destPos,length);
         */
        int arr[] = {0,1,2,3,4};
        int dest[] = new int[8];
        System.arraycopy(arr,0,dest,0,arr.length);
        System.out.println(Arrays.toString(dest));
        for (int i:dest) {
            System.out.print(i+" ");
        }
    }
}

获取系统时间毫秒数

System.currentTimeMillis():常用于计算系统运行时间

package commonclass.systemclass;

import java.util.concurrent.ForkJoinPool;

public class Demo02 {
    public static void main(String[] args) {
        long start=System.currentTimeMillis();
        for (int i = 0; i < 9999; i++) {
            for (int j = 0; j <99999 ; j++) {
                int result = i+j;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("系统用时:"+(end-start));
    }
}

垃圾回收器

System.gc():告诉垃圾回收器回收

package commonclass.systemclass;

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //垃圾回收器
    @Override
    protected void finalize() throws Throwable {
        System.out.println("回收了"+name+"/t"+age);
    }
}
package commonclass.systemclass;

public class Person {
    public static void main(String[] args) {
        //垃圾对象,没有用
        new Student("aaa",22);
        new Student("bbb",23);
        new Student("ccc",32);

        //通知垃圾回收器回收
        System.gc();
    }
}

退出JVM

exit():0表示正常退出,非0表示异常退出

package commonclass.systemclass;

public class Demo03 {
    public static void main(String[] args) {
        System.exit(0);
        //👆已经退出了 ,所以👇代码不执行
        System.out.println("geLaoTao");
    }
}

补充:后面还有流的笔记,会再完善System常用方法

posted @ 2021-10-26 08:40  葛老头  阅读(55)  评论(0编辑  收藏  举报