ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

常用类---Object

2021-03-28 18:01:48  阅读:96  来源: 互联网

标签:常用 String Object equals System --- println hashCode out


常用类—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("");
    }

标签:常用,String,Object,equals,System,---,println,hashCode,out
来源: https://blog.csdn.net/weixin_45658861/article/details/115284366

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有