9、 Object类中toString方法、equals方法、finalize方法、hashCode方法

API:

应用程序编程接口,整个JDK的类库就是一个javase的API。
每一个API都会配置一套API帮助文档

toString方法

1、源码

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

源代码上toString()方法的默认实现是:
类名@对象的内存地址转换为十六进制的形式
2、toString()方法的作用是什么?
toString()方法设计的目的:通过调用这个方法可以将一个j"ava对象"转换为"字符串"。
3、SUN公司开发Java语言的时候,建议所有的子类都去重写toString()方法。
toString()方法应该是一个简洁的、详实的、易阅读的。

//不重写toString()方法,输出的结果是类名@十六进制
public class Tedt01 {
    public static void main(String[] args) {
        MyTime m = new MyTime(1928,11,27);
        String s1 = m.toString();
        System.out.println(s1);
        //MyTime@7f31245a
    }
}

class MyTime{
    int year;
    int month;
    int day;

    public MyTime() {
    }

    public MyTime(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 + "日";
    }
    //进行方法重写之后输出:1928年11月27日
}

equals方法

1、源码

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

2、SUN公司设计equals方法的目的:通过equals方法判断两个对象是否相等。
3、我们会发现equals方法不够用!!
在Object中的equals方法中,默认使用的是 两个等号来判断两个java对象是否相等。
4、判断两个对象是否相等不能使用"==",因为等号比较的是两个对象的内存地址。我们相判断的是两个类的值是否相等。
所以我们需要重写equals方法。

equals如何对比两个对象是否一样?

package com.bjpowernode.javase.day16.homework;

public class Tedt01 {
    public static void main(String[] args) {
        MyTime m = new MyTime(1928,11,27);
        MyTime f = new MyTime(1928,11,27);
        boolean flag = m.equals(f);
        System.out.println(flag);


    }
}

class MyTime{
    int year;
    int month;
    int day;

    public MyTime() {

    }

    public MyTime(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    //重写equals方法
    public boolean equals(Object obj){
        //年相同,月相同,日相同,则两个对象相同
        int year1 = this.year;
        int month1 = this.month;
        int day1 = this.day;

        if (obj instanceof MyTime){
            MyTime t = (MyTime)obj;
            int year2 = t.year;
            int month2 = t.month;
            int day2 = t.day;
            if (year1 == year2 && month2 == month2 && day1 == day2) {
                return true;
            }
        }
        return false;
    }


}

对代码进行优化之后的equals方法:

public boolean equals(Object obj){
        if( ! (obj instanceof MyTime) || obj == null)
            return false;
        if (this == obj )
            return true;
        MyTime m = (MyTime) obj;
        //if (this.month == m.month && this.year == m.year && this.day == m.day){
        return (this.month == m.month && this.year == m.year && this.day == m.day);
    }

idea一键生成的equals方法:

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyTime myTime = (MyTime) o;
        return year == myTime.year && month == myTime.month && day == myTime.day;

那么String类有没有调用equals方法了?
1、String类已经重写了equals方法,比较两个字符串不能使用==,必须使用equals。equals是通用的。
2、String类已经重写了toString方法。

结论:
java中什么类型的数据可以使用两个等号判断
java中基本数据类型比较是否相等,使用==
java中什么类型的数据可以使用equals判断
java中所有的引用数据类型统一使用equals方法来判断是否相等

关于equals方法重写的练习:

package com.bjpowernode.javase.day16.work;

public class Test01 {

    public static void main(String[] args) {
        User u1 = new User("001",new Address("zhongguo","dalong","43hao"));
        User u2 = new User("001",new Address("zhongguo","dalong","43hao"));
        System.out.println(u1.equals(u2));
        //比较两个用户是否相同,需要比较姓名、住址是否相同。
    }


}

class User{
    //用户类
    //用户名字、地址
    String name;
    Address addr;

    public User() {
    }

    public User(String name, Address addr) {
        this.name = name;
        this.addr = addr;
    }
    //重写equals方法
    public boolean equals(Object o){
        if (this == o ) return true;
        if(o == null || !(o instanceof User)) return false;
        User u = (User)o;
        return this.name.equals(u.name) && this.addr.equals(u.addr);
    }
}

class Address{
    //地址类
    //街道、路、邮编
    String city;
    String street;
    String zipcode;
    public  Address(){}
    public Address(String city,String street,String zipcode){
        this.city = city;
        this.street = street;
        this.zipcode = zipcode;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    //重写equals方法
    public boolean equals(Object o){
        if (o == null || !(o instanceof Address)) return false;
        if (this == o) return true;
        Address a = (Address) o;
        if (this.city.equals(a.city)
                && this.street.equals(a.street)
                && this.zipcode.equals(a.zipcode)) return true;
        else return false;

    }
}


finalize方法

1、源码

protected void finalize() throws Throwable { }

GC负责调用finalize()方法
2、finalize()方法只有一个方法体,里面没有代码,而且这个方法是protected修饰的。
3、这个方法不需要程序员手动调用,JVM的垃圾回收器负责调用这个方法。
4、finalize()方法的执行时机:
当一个java对象即将被垃圾回收器回收的时候,垃圾回收器负责调用这个方法。
equals、toString方法需要程序员负责调用这个方法,finalize只需要重写,重写完将来会自动有程序调用。
5、finalize()方法实际上是SUN公司为java程序员准备的一个时机,垃圾销毁时机。如果希望在对象销毁时机执行一段代码的话,这段代码要写到finalize()方法当中。
6、类似于静态代码块:
static{

}
静态代码块在类加载时刻执行,并且只执行一次。
这是SUN准备的类加载时机


hashCode方法

1、源码:

public native int hashCode();

这个方法不是抽象方法,带有native关键字,底层调用的是C++程序。
2、hashCode方法返回的是哈希值:
实际上就是java对象的内存地址,经过哈希算法,得出的一个值。
所以hashCode方法的执行结果可以等同看成一个java对象的内存地址。
3、

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值