- System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有的。
方法名 | 说明 |
static void arraycopy(...) | 复制数组 |
static long currentTimeMillies(); | 获取当前系统时间,返回的时毫秒值 |
static void gc(); | 建议JVM赶快启动垃圾回收器回收垃圾 |
static void exit(int status) | 退出jvm,如果参数是0表示正常退出jvm,非0表示异常退出jvm。 |
//Demo04
public class Demo04 {
public static void main(String[] args) {
//arraycopy() 复制数组
//1src:源数组
//2srcPos:起始位置,首位为0
//3dest:目标数组
//4destPos:目标数组的位置,首位为0
//5length:长度
int[] a = {1,2,3,4,5,6,7,8,9,0};
int[] b = {0,9,8,7,6,5,4,3,2,1};
System.arraycopy(a,0,b,0,1);
for (int j : b) {
System.out.print(j + " ");
}
//Arrays.copyOf(a,2); 就是调用的System.arraycopy方法,此方法是一个本地方法,是用其他语言写的(C/C++),执行效率更高
//currentTimeMillis 可以输出系统时间毫秒 可以用来计算代码运行时间
System.out.println(System.currentTimeMillis());
long start = System.currentTimeMillis();
for (int i = 0; i < 9999; i++) {
for (int j = 0; j < 9999; j++) {
String c = ""+i+j;
}
}
long end = System.currentTimeMillis();
System.out.println("用时:"+(((double)end-(double) start)/1000));
System.out.println("=======================");
//建议JVM垃圾回收器回收垃圾
new Student("1", 2);
new Student("2", 2);
new Student("3", 2);
new Student("4", 2);
System.gc();
//退出JVM
System.exit(0);
System.out.println("程序结束了");
}
}
//Student
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name) {
this.name = name;
}
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);
}
}