Java-System

System类包含几个有用的类字段和方法。 它无法实例化。 System类提供的设施包括标准输入,标准输出和错误输出流; 访问外部定义的属性和环境变量; 加载文件和库的方法; 以及用于快速复制阵列的一部分的实用方法。

1. 字段

static PrintStream err “标准”错误输出流。
static InputStream in “标准”输入流。
static PrintStream out “标准”输出流。

2. System类常用方法

1. static long currentTimeMillis()
以毫秒为单位返回当前系统时间。

public class Demo2 {
    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        System.out.println(time/1000/60/60/24/365);//50 距1970年1月1日 8:00:00已过 50年
    }
}

2. static void exit​(int status)
终止当前运行的Java虚拟机。 通常传入0为正常状态,非0位异常状态。

  public class SystemDemo {
     public static void main(String[] args) {
         System.exit(0);
 }

3.static void gc()
运行垃圾收集器。

public class Demo2 {
    public static void main(String[] args) {
        new Person();
        new Person();
        new Person();
        new Person();
        System.gc();

    }

}

4. static void arraycopy​(Object src, int srcPos, Object dest, int destPos, int length)
将指定源数组中的数组从指定位置开始复制到目标数组的指定位置。
Object src:目标数组
int srcPos:原数组起始位置
Object dest:目标数组起始位置
int length:复制长度

import java.util.Arrays;

public class Demo2 {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = new int[10];
        int[] arr3 = new int[10];
        System.out.println(Arrays.toString(arr2));//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        System.arraycopy(arr1,1,arr2,6,3);
        System.out.println(Arrays.toString(arr1));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        System.out.println(Arrays.toString(arr2));//[0, 0, 0, 0, 0, 0, 2, 3, 4, 0]
        System.arraycopy(arr1,3,arr3,6,4);
        System.out.println(Arrays.toString(arr3));//[0, 0, 0, 0, 0, 0, 4, 5, 6, 7]
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值