4元素交换和gc()

78 阅读1分钟
/*
    实现数组中两个元素值的交换
    实现原理:
        借助中间变量
 */
public class Demo04ArraySwap {
    public static void main(String[] args) {
        int[] array = {10, 20};
        System.out.println("交换前数组内容: "+ Arrays.toString(array));

        int temp = array[0];//把数组array中0索引元素赋值给int变量temp
        array[0] = array[1];//把数组array中1索引元素赋值给数组array中0索引元素
        array[1] = temp;//把temp的值赋值给数组array中1索引元素

        System.out.println("交换后数组内容: "+ Arrays.toString(array));
    }
}
 
package com.itheima.aop;

/*
    System类 gc(): 运行垃圾回收器 JVM将从堆内存中清理对象,
        清理对象的同时会调用对象的finalize()方法,建议自定义类覆盖重写finalize()方法
        JVM的垃圾回收器是通过另一个线程开启的
*/
public class Demo09System {
    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
        //while(true){
            new Student();
        }
        System.gc();//建议运行垃圾回收器
        System.out.println("main...end...");
    }
}  
重写
public class Student {
    @Override
    protected void finalize() throws Throwable {
        System.out.println("对象被回收了...");
    }
}