JAVASE基础(十)

本文详细介绍了Java中的Object类方法,包括getclass、hashCode、toString、equals和finalize。接着讨论了包装类的概念、装箱拆箱、类型转换以及整数缓冲区。进一步讲解了String类的重要特性,如不可变性、常用方法和字符串操作,并提到了StringBuffer和StringBuilder。此外,还提及了BigDecimal类用于精确浮点数计算,Date类表示时间,Calendar类处理日期和时间,以及SimpleDateFormat类的日期格式化。最后提到了System类在获取系统属性方面的应用。
摘要由CSDN通过智能技术生成

三、Object类常用方法

(1)getclass()

public final Class<?> getClass(){}

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

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

package com.CommonClass.ObjectDemo01;

public class Student {
   
    private String name;
    private int age;

    public Student() {
   
    }

    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;
    }
}
package com.CommonClass.ObjectDemo01;

public class StudentText {
   
    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("同一类型");
        }else {
   
            System.out.println("不属于同一类型");
        }
    }
}

(2)hashCode()

public int hashCode(){}

返回对象的哈希码值。

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

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

        //hashCode方法
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        //与s1相同,因为把s1的地址赋给了s3
        Student s3 = s1;
        System.out.println(s3.hashCode());

(3)toString方法

public String toString(){}

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

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

    public String toString(){
   
        return name+":"+age;
    }
        //toString方法
        System.out.println(s1.toString());
        System.out.println(s2.toString());

(4)equals()方法

public boolean equals(Object obj){}

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

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

        //equals方法:判断两个对象是否相等
        System.out.println(s1.equals(s2));//false
        System.out.println(s1.equals(s3));//true
        Student s4 = new Student("王",21);
        Student s5 = new Student("王",21);
        System.out.println(s4.equals(s5));//false

equals()方法覆盖步骤:比较两个引用是否指向同一个对象,判断obj是否为null,判断两个引用指向的实际对象类型是否一致,强制类型转换,依次比较各个属性值是否相同。

    //重写equals方法
    @Override
    public boolean equals(Object obj) {
   
        //1.判断两个对象是否是同一个引用 this为当前对象,obj为参数比较对象
        if (this == obj){
   
            return true;
        }
        //2.判断obj是否为null
        if (obj == null){
   
            return false;
        }
        //3.判断是否是同一个类型
//        if (this.getClass() == obj.getClass()){
   
//
//        }
        //instanceof 判断对象是否是某种类型
        if (obj instanceof Student){
   
            //4.强制类型转换
            Student s = (Student)obj;
            //5.比较属性
            if (this.name.equals(s.getName())&&this.age==s.getAge()){
   
                return true;
            }
        }
        return false;
    }

        Student s4 = new Student("王",21);
        Student s5 = new Student("王",21);
        System.out.println(s4.equals(s5));//true

(5)finalize()方法

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

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

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

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

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

    @Override
    protected void finalize() throws Throwable {
   
        System.out.println(this.name+"对象被回收了");
    }
}
package com.CommonClass.ObjectDemo01;

public class StudentText2 {
   
    public static void main(String[] args) {
   
//        Student s1 = new Student("a",18);
//        Student s2 = new Student("b",20);
//        Student s3 = new Student("c",21);
//        Student s4 = new Student("d",22);
//        Student s5 = new Student("e",23);
        new Student("a",18);
        new Student("b",20);
        new Student("c",21);
        new Student("d",22);
        new Student
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值