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();