Java的System类简介

System类不可实例化

System作为系统类,在ava.lang包中

System类的构造器由private修饰,不允许被实例化,类中的方法也都是static修饰的静态方法。

字段

  • public final static InputStream in;   标准输入流
  • public final static PrintStream out; 标准输出流
  • public final static PrintStream err;  标准错误流

System中的out和in都不是内部类,而是货真价实的字段变量.

out是PrintStream的final static修饰的变量字段,也就是说它可以调用PrintStream类的方法.

println是PrintStream的一种输出方法,通常采用System.out.println()在控制台输出内容.

常用方法:

  • arraycopy -  数组拷贝

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

int[] a = {1,2,3};
int[] b = new int[a.length];
System.arraycopy(a, 0, b, 1, 2);
for(int i : b) {
	System.out.print(i);
}
  • currentTimeMillis - 返回毫秒数

该方法的作用是返回当前的计算机时间

时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数

long t1 = System.currentTimeMillis();
try {
	Thread.sleep(1000);
} catch (Exception e) {
			
}
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);
  • exit - 退出程序

这是唯一一个能够退出程序并不执行finally的情况

try {
        System.out.println("this is try");
	    System.exit(0);
} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
} finally {
	    System.out.println("this is finally");
}

gc - 垃圾回收

System.gc();

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值