System类
System表示当前运行的JVM,提供标准输入、输出和错误输出流对象。只读,不能实例化。
api文档说明如下:
成员属性如下:
提供的方法如下:
以下通过一个例子来说明常用属性和方法:
public static void main(String[] args) {
checkAge(-2);
checkAge(200);
checkAge(25);
// 获取当前系统的时间戳
long current = System.currentTimeMillis();
System.out.println("当前时间戳为:" + current);
Date time = new Date(current);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
String time_str = sdf.format(time);
System.out.println(time_str);
// 垃圾回收
System.gc();
// 获取系统属性
Properties properties = System.getProperties();
System.out.println(properties);
// 获取指定属性
String res = System.getProperty("java.runtime.name");
System.out.println(res);
}
// 定义一个私有方法
private static void checkAge(int age) {
if (age < 0 || age > 130) {
System.err.println("年龄不合法!");
} else {
System.out.println("当前年龄为:" + age);
}
}
执行,控制台:
其他方法可以查阅jdk的api文档。