【Java学习笔记】System类

System 类

常用方法

方法描述
System.exit()退出当前程序
System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝长度)复制数组元素,比较适合底层调用
System.currentTimeMillis()返回当前时间距离 1970-1-1 的毫秒数
System.gc()执行垃圾回收机制

一、exit()

传入参数 0:表示正常退出

代码示例

public class main {
    public static void main(String[] args) {
        System.out.println("hello world");
        System.exit(0);
        System.out.println("程序继续执行...");
    }
}

// 输出结果
hello world

二、System.arraycopy()

复制数组元素,比较适合底层调用

使用方法:System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝长度)

代码示例

import java.util.Arrays;

public class main {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4};
        int[] arr_new = new int[arr.length]; // 此时 arr_new 为 {0, 0, 0, 0}
        System.arraycopy(arr,0,arr_new,1,3);
        System.out.println(Arrays.toString(arr_new));
    }
}

// 输出结果
[0, 1, 2, 3]

代码说明

  • 源数组:arr

  • 源数组拷贝的起始位置:0

  • 目标数组:arr_new

  • 目标数组拷贝的起始位置:1

  • 拷贝长度:3


三、System.currentTimeMillis()

返回当前时间距离 1970-1-1 的毫秒数

代码示例

public class main {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}

// 输出结果
1749372542217

四、System.gc()

主动触发垃圾回收机制,底层会默认调用finalize()方法执行垃圾回收机制

代码示例

public class finalize {
    public static void main(String[] args) {
        finals finals = new finals(18);
        finals = null;
        System.gc();
    }
}
class finals{
    int age;

    public finals(){

    }

    public finals(int age) {
        this.age = age;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("调用finalize回收对象");
    }
}

// 输出
调用finalize回收对象
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackson凌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值