java笔记--day11--类object之hashCode() and getClass()

本文深入介绍了Java中Object类的核心方法,如hashCode()和getClass()。通过实例演示了这些方法的具体用法,帮助读者理解对象的唯一标识及获取对象运行时类的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 1 Object类的概述
    • 类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。
    • Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
  • 2 成员方法
    • public int hashCode()
      public final Class getClass()
      public String toString()
      public boolean equals(Object obj)
      protected void finalize()
      protected Object clone()
    • 本小节讲解方法hashCode()和getClass()
  • 3 public int hashCode()
    • 暂时当作对象的地址
  • 4 public final Class getClass()
    • 可以看出,返回此 Object 的运行时类。
    • 这里的Class是一个类,该类中有一个public string getName()方法
      该类以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。
  • 5 代码实现:
public class StudentDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(s1.hashCode());// 57410271
        Student s2 = new Student();
        System.out.println(s2.hashCode());// 674267194
        Student s3 = s1;
        System.out.println(s3.hashCode());// 57410271

        Student s = new Student();
        System.out.println(s.getClass());// class xsh_itcast_02.Student
        System.out.println(s.getClass().getName());// xsh_itcast_02.Student
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值