JavaSE:Object类中的toString ,equals,hashcode方法

目录

 Object类中的方法

 toString方法

 equals方法

 finalize方法

 HashCode方法


 Object类中的方法

Object 类位于 java.lang 包中,编译时会自动导入,我们创建一个类时,如果没有明确继承一个父类,那么它就会自动继承 Object,成为 Object 的子类

隐式继承:

public class 类名{}

显示继承

public class 类名 extends Object{}
方法名格式作用
clone
object.clone()
创建并返回一个对象的拷贝
equals
object.equals(Object obj)
比较两个对象是否相等
finalize
protected void finalize()
当 GC (垃圾回收器)确定不存在对该对象的有更多引用时,由对象的垃圾回收器调用此方法。
getClass
object.getClass()
获取对象的运行时对象的类
hashCode
object.hashCode()
获取对象的 hash 值
notify
public final void notify()
唤醒在该对象上等待的某个线程
notifyAll
public final void notifyAll()
唤醒在该对象上等待的所有线程
toString
object.toString()
返回对象的字符串表示形式
wait
public final void wait()
让当前线程进入等待状态。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法

 toString方法

public String toString ()

返回对象的字符串表示形式,以文本方式表示这个对象(建议子类重写)

未重写时的结果,得到的是一个默认实现,即对象的内存地址,如下图:

public class Time {
   public static void main(String[] args) {

      //创建对象并传入参数值

      TheTime t = new TheTime(2022, 4, 7);

      //创建一个String 对象 t1 用于接收对象 t 调用toString方法后得到的值

      String t1 = t.toString();
      System.out.println(t1);
   }
}

class TheTime{
      int year;
      int month;
      int day;

   //构造方法

   public TheTime(){

   }

   public TheTime(int year , int month , int day ){
      this.year = year ;
      this.month = month ;
      this.day = day ;
   }
}

重写后:

public class Time {
   public static void main(String[] args) {
      //创建对象并传入参数值
      TheTime t = new TheTime(2022, 4, 7);
      //创建一个String 对象 t1 用于接收对象 t 调用toString方法后得到的值
      String t1 = t.toString();
      System.out.println(t1);
   }
}

class TheTime{
      int year;
      int month;
      int day;
   //构造方法
   public TheTime(){

   }
   public TheTime(int year , int month , int day ){
      this.year = year ;
      this.month = month ;
      this.day = day ;
   }
   //重写toString方法
   public String toString(){
      return this.year + "/"+ this.month + "/" + this.day;
   }
}

 equals方法

 默认实现:

public boolean equals (Object obj){
    return (this == obj);
} 

注意:

==(双等号)判断两个基本数据类型的值是否相等
equals()判断两个java对象是否相等

重写前(t1和t2传入的参数值一样但输出是false):

原因:重写前,在equals方法中使用的==双等号

public class Time {
   public static void main(String[] args) {
      //创建对象并传入参数值
      TheTime t1 = new TheTime(2022, 4, 7);
      TheTime t2 = new TheTime(2022, 4, 7);
      t1.equals(t2);
      boolean T = t1.equals(t2);
      System.out.println(T);
   }
}

class TheTime{
      int year;
      int month;
      int day;
   //构造方法
   public TheTime(){

   }
   public TheTime(int year , int month , int day ){
      this.year = year ;
      this.month = month ;
      this.day = day ;
   }
}

 

重写后:

public class Time {
   public static void main(String[] args) {
      //创建对象并传入参数值
      TheTime t1 = new TheTime(2022, 4, 7);
      TheTime t2 = new TheTime(2022, 4, 7);
      t1.equals(t2);
      boolean T = t1.equals(t2);
      System.out.println(T);
   }
}

class TheTime{
      int year;
      int month;
      int day;
   //构造方法
   public TheTime(){

   }
   public TheTime(int year , int month , int day ){
      this.year = year ;
      this.month = month ;
      this.day = day ;
   }
   //重写equals方法
   public boolean equals (Object obj){
      //获取t1
      int year = this.year;
      int month = this.month;
      int day = this.day;
      //向下转型
      if(obj instanceof TheTime){
         TheTime t = (TheTime)obj;
         int year1 = t.year;
         int month1 = t.month;
         int day1 = t.day;
         if(year == year1 && month == month1 && day == day1){
            return true;
         }
      }
      return false;
   }
}

 finalize方法

Object finalize() 方法用于实例被垃圾回收器回收的时触发的操作。

当 GC (垃圾回收器) 确定不存在对该对象的有更多引用时,对象的垃圾回收器就会调用这个方法

(1)finalize只有一个方法体,用protected修饰(JDK高版本此方法是过时的)

(2)不需要手动调用,JVM垃圾回收器负责调用

(3)如果垃圾数量太少,太小可能不会执行

(4)可以使用  System.gc() 建议启动垃圾回收器 

数量太小时,不会执行:

public class 垃圾回收 {
    public static void main(String[] args) {
    Rubbish R = new Rubbish();

    }
}

class Rubbish{
    protected void finalize(){
        System.out.println("正在回收");
    }
}

数量达到时:

public class 垃圾回收 {
    public static void main(String[] args) {
        for (int i = 0; i <= 1000; i++) {
            Rubbish R = new Rubbish();
            R.finalize();
        }
    }
}

class Rubbish{
    protected void finalize(){
        System.out.println("正在回收");
    }
}

 HashCode方法

hashCode()方法的返回是哈希码,即一个java对象的内存地址,经过哈希算法,得出的一个值,可以看作一个java对象的内存地址

public class 哈希 {
    public static void main(String[] args) {
        Object obj1 = new Object();
        System.out.println(obj1.hashCode());

        Object obj2 = new Object();
        System.out.println(obj2.hashCode());

        Object obj3 = new Object();
        System.out.println(obj3.hashCode());
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java从跨平台到跨行业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值