常用类---Object

常用类—Object(Objects)

1. toString()

返回对象的字符串表示 类的全路径+”@“+对象hash值

​子类中可以进行重写
 public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

2. equals()

- 自反性 x.equals(x) 结果为true
- 对称性 x.equals(y) 结果与y.equals(x)结果一致
- 传递性  x.equals(y) 为true 并且  y.equals(z)也为 true 那么此时 x.equals(z)为true
- 一致性 返回的结果应该始终一致

hashcode 哈希码

每个对象都有唯一的hash值 能够唯一的代表此对象

两个对象equals 那么这两个对象的hash值必须一样,在集合中使用


如果要重写equals方法 一定要重写hashCode方法

Stu类重写

 @Override
    public boolean equals(Object obj) {
        Stu op;
        if (obj instanceof Stu) {
            op = (Stu) obj;
            return this.name.equals(op.getName()) && this.age == op.getAge();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.name.hashCode() + age;
    }

测试类

public static void main(String[] args) {
        Object stu1 = new Stu("tom", 12);
        Stu stu2 = new Stu("tom", 12);
        System.out.println(stu1.equals(stu2));
        System.out.println(stu1.hashCode());
        System.out.println(stu2.hashCode());
        System.out.println(stu1 == stu2);
        String str1 = new String("abc");
        String str2 = new String("abc");
        // String重写了equals方法 比较的是字符内容是否相等
        System.out.println(str1.equals(str2));
        System.out.println(str1 == str2);
    }

3. hashCode()

- hash值能够唯一的代表此对象
- 两个对象equals 那么这两个对象的hash值必须一样,重写equals一定要重写hashCode方法
- 两个对象不相等 hashCode值应该不同 

native 表示本地方法  方法实现不是java实现的 而是C/C++来实现的
public native int hashCode();

4. finalize()

当GC开始回收这个对象的时候 会自动调用

5. clone()

  • object的科隆是浅克隆
  • 克隆对象 : 克隆的对象和被克隆的对象是指向同一块地址空间的,修改原有的对象的属性,克隆对象的属性也会被改变
public class ObjectDemo implements Cloneable {
    private int num;
    // 如果是引用类型,并未对成员变量进行克隆  指向的是同一个对象
    private Stu stu;

    public static void main(String[] args) {
        ObjectDemo objectDemo = new ObjectDemo();
        Object clone = null;
        try {
            objectDemo.num = 10;
            objectDemo.stu = new Stu();
            clone = objectDemo.clone();
            objectDemo.stu.setAge(20);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println(((ObjectDemo) clone).num);
        System.out.println(((ObjectDemo) clone).stu.getAge());
        System.out.println(clone);
        System.out.println(objectDemo.hashCode());
        System.out.println(clone.hashCode());
        System.out.println(clone.equals(objectDemo));
    }
}

Objects类

  • 工具类 ,为了优雅的解决使用Object类方法时会出现的空指针问题
  • 内部的方法都是静态方法
public static void main(String[] args) {
        String str = null;
        //1,比较对象是否相等
        System.out.println(Objects.equals("", null));
        //3, 获取hashcode值
        System.out.println(Objects.hashCode(str));
        int[] ints1 = {1, 2, 3, 45};
        int[] ints2 = {1, 2, 3, 45};
        System.out.println("数组相等:" + Objects.equals(ints1, ints2));
        // 4,deepEquals()常用来比较数组元素是否一一相等
        System.out.println(Objects.deepEquals(ints1, ints2));
        // 5, isNull 是否为null 如果为null返回结果时true,否则返回false
        boolean isNull = Objects.isNull("");
    }
上一篇:Java中的Set对象去重


下一篇:代码安全 | 第十七期:对象只定义了Equals和Hashcode方法之一的漏洞