System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有,不需要创建对象,属性方法都是静态的
public class TestSystem {
public static void main(String[] args) {
//arraycopy实现数组复制
//src:原数组
//srcPos:从哪个位置开始复制
//dest:目标数组
//destPos:目标数组位置
//length:复制长度
int[] a = {1,2,3,4,5}; //原数组
int[] b = new int[5];
System.arraycopy(a,2,b,2,2);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
//获取当前系统时间,返回毫秒值 System.currentTimeMillis()
System.out.println(System.currentTimeMillis());
//实现计时
long start = System.currentTimeMillis();
/* 代码区 */
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - start));
创建一个学生测试类用于垃圾回收
public class TestStudent {
private String name;
private int age;
//构造方法
public TestStudent(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 "TestStudent{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//finalize(): 垃圾回收时执行的方法
@Override
protected void finalize() throws Throwable {
System.out.println("回收了" + name + "" + " " + age);
}
}
//垃圾回收
new TestStudent("qq",11);
new TestStudent("ww",22);
new TestStudent("rr",33);
System.gc(); //告诉垃圾回收器回收
//退出JVM
System.exit(0);
System.out.println("程序结束");