System类
- System系统类,主要用于获取系统的属性数据和其他操作,构造方法是私有的。
![在这里插入图片描述](https://img-blog.csdnimg.cn/431995565171423ea855a88cfdba52a9.png)
示例:
Student类(回收垃圾会用到)
package com.jacyzhu.system;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name +", age=" + age + "]";
}
@Override
protected void finalize() throws Throwable {
System.out.println("回收了" + name + "" + " " + age);
}
}
测试类
- 数组的复制:
System.arraycopy(src, srcPos, dest, destPos, length);
- 获取毫秒数:
System.currentTimeMillis();
- 回收垃圾:
System.gc();
- 退出JVM:
System.exit(0);
package com.jacyzhu.system;
public class demo01 {
public static void main(String[] args) {
int[] arr = {20, 18, 15, 5, 35, 26, 45, 90};
int[] dest = new int[8];
System.arraycopy(arr, 4, dest, 0, 4);
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
System.out.println(System.currentTimeMillis());
long start = System.currentTimeMillis();
for (int i = 0; i < 9999999; i++) {
for (int j = 0; j < 9999999; j++) {
int result = i+j;
}
}
long end = System.currentTimeMillis();
System.out.println("用时:"+ (end-start));
new Student("aaa", 19);
new Student("bbb", 20);
new Student("ccc", 21);
System.gc();
System.exit(0);
System.out.println("程序结束了...");
}
}
运行结果:
35
26
45
90
0
0
0
0
1659953292037
用时:5
回收了ccc 21
回收了bbb 20
回收了aaa 19