深拷贝
浅拷贝
垃圾回收
package com.demo.obj; /** * 学习Object类 * native 本地栈方法, 方法使用使用c语言中实现的方法 * hashcode 就是一个对象在内存中地址 * toString * clone * equals * finalize * @author Administrator * * // 再次强调静态方法中不能使用this关键字 * */ public class ObjectTest extends Object implements Cloneable { public static void main(String[] args) { // 创建一个对象 ObjectTest obj = new ObjectTest(); // 使用System.out.println打印一个对象的时候会自动调用该对象的toString()方法 System.out.println(obj); // @15db9742 // 使用hashcode方法 System.out.println(Integer.toHexString(obj.hashCode())); // 使用toString方法 // System.out.println(obj.toString()); Object obj1 = obj.cloneObject(); System.out.println(obj1); // @6d06d69c Object obj3 = obj; // equals方法 (==) 比较2个对象的引用(内存地址)是否相同 System.out.println(obj == obj1); // false 内存地址不同 System.out.println(obj3 == obj); // true 内存地址相同 System.out.println(obj.equals(obj1)); System.out.println(obj3.equals(obj)); // obj1=null; System.gc(); // 手动调用系统中的垃圾回收器 // 需要使用垃圾回收触发finalize调用 } @Override protected void finalize() throws Throwable { System.out.println("== 对象的销毁 =="); super.finalize(); // 调用父类中gc回收对象的时候就会调用 } //@Override // 重写了父类中toString()方法 /*public String toString() { return "ObjectTest []"; }*/ public Object cloneObject() { Object obj = null; try { // java.lang.CloneNotSupportedException: com.demo.obj.ObjectTest // 必须实现Cloneable接口 obj = this.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } }
原文:https://www.cnblogs.com/sunBinary/p/10464628.html