首页 > 技术文章 > Java的System类

fangweicheng666 2021-07-15 19:08 原文

Java的System类

  • System系统类,主要用于获取系统的属性数据和其他操作,构造方法是私有的
方法名 说明
static void arraycopy(...); 复制数组
static long currentTimeMillis(); 获取当前系统时间,返回的是毫秒值
static void gc(); 建议JVM赶快启动垃圾回收回收垃圾
static void exit(int status); 退出jvm,如果参数是0表示正常退出jvm,非0表示异常退出jvm
package com.cnblogs;


import java.util.Arrays;

//本类用于实现
public class Application {
    public static void main(String[] args) {
        //arraycopy,数组的复制
//        System.arraycopy(src,srcPos,dest,destPos,length);
        //src:源数组   srcPos:从哪个位置开始复制
        //dest:目标数组     destPos:目标数组的位置     length:复制的长度
        int[] arr1 = {1,2,3,4,5,6,7,8,9};
        int[] arr2 = {11,12,13,14,15,16,17,18,19};
        System.arraycopy(arr1,0,arr2,0,5);
        System.out.println(Arrays.toString(arr2));//[1, 2, 3, 4, 5, 16, 17, 18, 19]

        System.out.println(System.currentTimeMillis());//1626331491516可以用于计时。

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

        new Student("柒", 19);
        new Student("柒", 20);
        new Student("柒", 21);
        System.gc();//

        //退出jvm
        System.exit(0);
        System.out.println("看看能不能执行");//这一行代码不执行了,已经退出jvm了

    }
}

package com.cnblogs;
/*
student类
 */
public class Student{
    private String name;
    private int age;

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

    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;
    }

    //重写finalize方法
    protected void finalize()throws Throwable{
        System.out.println(this.name + "垃圾被回收了!!!");
    }

}

推荐阅读