标签:
2016-07-24
1,Object类的常用方法介绍
finalize() gc() 这两个方法我们不要试图去控制,他们是由Java虚拟机
控制的,我们只能建议gc()去执行finalize(),但具体是否执行,只有
Java虚拟机才知道,因为这是有Java虚拟机的算法决定的。
package com.java1995; public class TestClone { public static void main(String[] args) { Student s1 = new Student(); s1.setName("老王"); s1.setAge(29); s1.setSex("男"); System.out.println(s1); try { Student s2 = s1.clone(); System.out.println(s2); System.out.println(s1 == s2); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Student implements Cloneable { private String name; private String sex; private int age; public Student clone() throws CloneNotSupportedException { return (Student) super.clone(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
package com.java1995; public class TestEquals_HashCode { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); System.out.println(o1.equals(o2)); System.out.println(o1.hashCode() + "," + o2.hashCode()); } }
package com.java1995; public class TestFinalize { private int id; public TestFinalize(int id) { this.id = id; } public static void main(String[] args) { for (int i = 0; i < 10000; i++) { new TestFinalize(i); /** * 建议虚拟机执行垃圾回收,但是不保证执行, 也不保证回收所有垃圾。 */ System.gc(); } } @Override public void finalize() { System.out.println(id + "正在被执行finalize方法"); } }
【参考资料】
标签:
原文地址:http://www.cnblogs.com/cenliang/p/5701085.html