Object类常用方法(getClass(),hashCode(),toString(),equals()...)

Object类常用方法

1.getClass()

package com.object.demo01;

public class Student {

}

测试类

package com.object.demo01;

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        Class class1= s1.getClass();
        Class class2 = s2.getClass();
        System.out.println(class1);//class com.object.demo01.Student
        System.out.println(class2);//class com.object.demo01.Student
        if(class1==class2){
            System.out.println("相同");
        }
    }

}

2.hashCode()方法

public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        Student student1 = new Student();
        int i2 = student.hashCode();
        int i3 = student1.hashCode();
        System.out.println(i2==i3);//false
        Student student2 = student;
        System.out.println(student2.hashCode()==student.hashCode());//true
    }
}

3.toString()方法
public class Test {
    public static void main(String[] args) {
        Student lxy = new Student("lxy");//com.object.demo04.Student@1b6d3586
        System.out.println(lxy.toString());
    }
}
输出结果中的com.object.demo04.Studen为该对象所属的类,@后的值为该对象所属哈希值的十六进制数
4.equals()方法
public class Test {
    public static void main(String[] args) {
        Student student = new Student("lxy");
        Student student1 = new Student("lxy");
        //System.out.println(student.equals(student1));//false,因为对象的地址不同(此时未重写equals方法)
        System.out.println(student.equals(student1));//true,
        // 因为重写了equals方法,重写了之后,如果为同一个类型的对象,就比较该对象的属性是否相同,如果相同,则返回true
        //重写方法如下:
        /*
     @Override
    public boolean equals(Object object) {

        if(this==object){//判断两个对象是否是同一个引用
            return true;
        }
        if(object==null){//判断o是否为空
            return false;
        }
        if(object instanceof Student){//判断对象是否是某种类型
            //强制类型转换
            Student s = (Student)object;
            if(this.name.equals(s.getName())){
                return true;
            }
        }
        return false;
    }
         */
    }
}

5.finalize()方法

finalize()方法的解释:

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

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

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

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

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

public class Test {
    public static void main(String[] args) {
        Student lxy = new Student("lxy");//此对象未被回收,因为有引用指向此对象
         new Student("yuan");//此对象被回收了,因为没有有效引用指向此对象
        System.out.println("通知JVM回收垃圾");
        System.gc();//该方法用来通知JVM回收垃圾
        //在Student类中重写finalize()方法:
        /*
            @Override
    protected void finalize() throws Throwable {
        System.out.println(this.name+"被回收");
    }
         */

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yuan_boss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值