Object类

超类、基类,所以类的直接或间接父类,位于继承树的最顶层

任何类,如没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承

Object类中所定义的方法,是所有对象都具备的方法

Object类型可以存储任何对象

  • 作为参数,可接受任何对象

  • 作为返回值,可返回任何对象

getClass()方法

public final Class<?> getClass(){}

返回引用中存储的实际对象类型

应用:通常用于判断两个引用中实际存储对象类型是否一致

public class Student {
   private String name;
   private int age;
   public Student(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;
  }
}
public class Test {
   public static void main(String[] args) {
       Student s1 = new Student("aaa", 20);
       Student s2 = new Student("bbb", 22);
       //判断s1和s2是不是同一类型
       Class class1 = s1.getClass();
       Class class2 = s2.getClass();
       if (class1 == class2){
           System.out.println("s1和s2属于同一类型");
      }else {
           System.out.println("s1和s2不属于同一类型");
      }
  }
}
/*
s1和s2属于同一类型
*/

hashCode()方法

public int hashCode(){}

返回该对象的哈希码值

哈希值根据对象的地址字符串数字使用hash算法计算出来的int类型的数值

一般情况下相同对象返回相同哈希值

public class TesthashCode {
   public static void main(String[] args) {
       Student s1 = new Student("aaa", 20);
       Student s2 = new Student("bbb", 22);

       System.out.println(s1.hashCode());
       System.out.println(s2.hashCode());
       Student s3 = s1;
       System.out.println(s3.hashCode());
  }
}
/*
460141958
1163157884
460141958
*/

toString()方法

public String toString(){}

返回该对象的字符串表示(表现形式)

可以根据程序需求覆盖该方法,如展示对象各个属性值

可根据需要进行重写

equals()方法

public boolean equals (Object obj){}

默认实现为(this==obj),比较两个对象地址是否相同

可进行覆盖,比较两个对象的内容是否相同

equals()方法覆盖步骤

  • 比较两个引用是否指向同一个对象

  • 判断obj是否为null

  • 判断两个引用指向的实际对象类型是否一致

  • 强制类型转换

  • 依次比较各个属性值是否相同

    public boolean equals(Object obj) {
       if (this==obj){
           return true;
      }
       if (obj==null)
           return false;
//       if (this.getClass()==obj.getClass())
//       instenceof 判断对象是否是某种类型
       if (obj instanceof Student){
           Student s = (Student) obj;
           if (this.name.equals(s.getName()) && this.age==s.getAge()){
               return true;
          }
      }
       return false;
  }
Student s3 = new Student("xiix",10);
Student s4 = new Student("xiix",10);
System.out.println(s3.equals(s4));
//true

finalize()方法

当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列

垃圾对象:没有有效引用指向此对象时,为垃圾对象

垃圾回收:由GC销毁垃圾对象,释放数据存储空间

自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象

手动回收机制:使用System.gc();通知JVM执行垃圾回收

 protected void finalize() throws Throwable {
       System.out.println(this.name+"对象被回收");
  }
public class TestFinalize {
   public static void main(String[] args) {
//       Student s1 = new Student("aa", 20);
////       Student s2 = new Student("bb", 20);
////       Student s3 = new Student("cc", 20);
////       Student s4 = new Student("ee", 20);
////       Student s5 = new Student("ff", 20);
       new Student("aa", 20);
       new Student("bb", 20);
       new Student("cc", 20);
       new Student("ee", 20);
       new Student("ff", 20);
       //回收垃圾
       System.gc();
       System.out.println("回收垃圾");
  }
}
/*
回收垃圾
aa对象被回收
ff对象被回收
ee对象被回收
cc对象被回收
bb对象被回收
*/
 

最新文章

  1. PHP判断sql语句是否执行成功
  2. 怎样将runlmbench 获取的数值传给上层app
  3. iOS学习37数据处理之CoreData
  4. VS 2010 编译 Openssl
  5. 一次简单的MySQL数据库导入备份
  6. iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:
  7. svn提交时强制添加注释 (转)
  8. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(6)
  9. Cheatsheet: 2014 08.01 ~ 08.31
  10. JVM 性能调优实战之:一次系统性能瓶颈的寻找过程
  11. uva301 - Transportation
  12. __attribute__ ((section(&quot;.text&quot;)))的测试
  13. JavaWeb 后端 &lt;十四&gt; 文件上传下载
  14. Python爬虫番外篇之Cookie和Session
  15. ●BZOJ 1042 [HAOI2008]硬币购物
  16. RSA算法原理——(1)目前常见加密算法简介
  17. 素数问题练习_HDOJ1262
  18. Codeforces Round #412 B. T-Shirt Hunt
  19. 设计模式——适配器模式(type-c转3.5mm耳机口)
  20. kafka基本概念

热门文章

  1. 我居然不知道Vue3可以使用hooks函数实现代码复用?
  2. angular8表格文件上传并渲染到页面(上传文本与表格,表格有两种写法--之二)
  3. Python3+Selenium3自动化测试-(准备)
  4. Angularjs的重要概念
  5. 结构性模式 - 适配器模式Adapter
  6. 一次代码重构 JavaScript 图连通性判定
  7. Python3中的“加和”函数
  8. 不花钱几分钟让你的站点也支持https
  9. 获取Excel列标
  10. 复制内容到剪切板通用的js方法