一、System类概述
在java.lang包下,所以使用时不需要导包
System类不能实例化,也就是不能创建对象
他的成员都被静态修饰了,所以只需要类名.方法就可以访问
重点学习两个方法
1.exit
非零状态码表示异常终止,那么零就是正常终止
代码演示:
没有使用exit:
public class SystemDemo {
public static void main(String[] args) {
System.out.println("开始");
System.out.println("结束");
}
}
输出:
开始
结束
使用了exit:
public class SystemDemo {
public static void main(String[] args) {
System.out.println("开始");
System.exit(0);
System.out.println("结束");
}
}
输出:
开始
2.currentTimeMills
注意返回的是1970年1月1日之间的毫秒值
我们用代码来求一下现在的时间距离1970年1月1日有多少年
public class currentDemo {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365+"年");
}
}
输出:
51.61479521369228年
用currentTimeMills来测代码执行耗费了多少时间