System类(数组的复制、获取毫秒数、回收垃圾、退出JVM)

System类

  • System系统类,主要用于获取系统的属性数据和其他操作,构造方法是私有的。
    在这里插入图片描述

示例:

Student类(回收垃圾会用到)

package com.jacyzhu.system;

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

    // 无参构造
    public Student() {
    }

    // 有参构造
    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;
    }

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

    @Override
    protected void finalize() throws Throwable {
        System.out.println("回收了" + name + ""  + "   " + age);
    }
}

测试类

  • 数组的复制:System.arraycopy(src, srcPos, dest, destPos, length);
  • 获取毫秒数:System.currentTimeMillis();
  • 回收垃圾:System.gc();
  • 退出JVM:System.exit(0);
package com.jacyzhu.system;

public class demo01 {
    public static void main(String[] args) {
        // 1.arraycopy:数组的复制
        // src:源数组
        // srcPos:从哪个位置开始复制 0
        // dest:目标数组
        // destPos:目标数组的位置
        // length:复制的长度
        int[] arr = {20, 18, 15, 5, 35, 26, 45, 90};
        int[] dest = new int[8];
        System.arraycopy(arr, 4, dest, 0, 4);
        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }

        // 2.获取毫秒数
        System.out.println(System.currentTimeMillis()); // 1659953292037

        long start = System.currentTimeMillis();
        for (int i = 0; i < 9999999; i++) {
            for (int j = 0; j < 9999999; j++) {
                int result = i+j;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("用时:"+ (end-start)); // 5

        // System.gc();告诉垃圾回收器回收
//        Student s1 = new Student("aaa", 19);
//        Student s2 = new Student("bbb", 20);
//        Student s3 = new Student("ccc", 21);

        // 没有被引用
        new Student("aaa", 19);
        new Student("bbb", 20);
        new Student("ccc", 21);

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

        // 4.退出JVM
        System.exit(0);
        System.out.println("程序结束了...");
    }
}
运行结果:
35
26
45
90
0
0
0
0
1659953292037
用时:5
回收了ccc   21
回收了bbb   20
回收了aaa   19
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值