文章目录
System类
概述
System类包含一些有用的类字段和方法,在java.lang包中,System代表的是系统,系统级别的很多属性和控制方法都放在该类的内部。System类的构造方法都是private的,因此它是无法创建对象,即无法实例化的。System类中的成员变量和方法都是静态的,调用时很方便。
System类中的常用方法
方法名 | 功能 |
---|---|
currentTimeMillis() | 返回当前系统时间,精确到毫秒,自1970年1月1日0时0分0秒至今 |
nanoTime() | 返回最精确的当前时间,精确到纳秒 |
arraycopy() | 将一个数组拷贝到一个新的数组中 |
identityHashCode() | 根据对象内存地址计算其哈希值,与HashCode()默认值一样 |
gc() | 启动jvm垃圾回收机制 |
exit() | 终止当前jvm |
代码示例
import java.util.Arrays;
public class SystemDemo {
public static void main(String[] args) {
//返回当前系统时间,精确到毫秒,自1970年1月1日0时0分0秒至今
System.out.println(System.currentTimeMillis());//1605808163233
//返回最精确的当前时间,精确到纳秒
System.out.println(System.nanoTime());//265775562376000
//将一个数组拷贝到一个新的数组中
int [] str = new int[3];
Arrays.fill(str,3);
int [] str1 = new int[10];
System.arraycopy(str,0,str1,0,str.length);
System.out.println(Arrays.toString(str1));//[3, 3, 3, 0, 0, 0, 0, 0, 0, 0]
//根据对象内存地址计算其哈希值,与HashCode()默认值一样
System.out.println(System.identityHashCode(str1));//460141958
//启动jvm垃圾回收机制
System.gc();
//终止当前jvm
System.exit(5);
}
}