Objects工具类

重点⽅法
1.equals
⽤于字符串和包装对象的⽐较,先⽐较内存地址,再⽐较值
2.deepEquals
数组的⽐较,先⽐较内存地址,再⽐较值,String/char/byte/int数组, 或者包装类型Integer等数组
3.hashCode
返回对象的hashCode,若传⼊的为null,返回0
4.hash
传⼊可变参数的所有值的hashCode的总和,底层调⽤Arrays.hashCode

package Collections;

import java.util.Objects;

public class ObjectTest {
    public static void main(String [] args){
        String str1 = "我要好好学习";
        String str2 = "我要好好学习.";
        String str3 = "我要好好学习";
        System.out.println(Objects.equals(str1,str2));//false
        System.out.println(Objects.equals(str1,str3));//true
        String [] arr1 = {"aa","bb","cc"};
        String [] arr2 = {"aa","bb","cc"};
        System.out.println(Objects.equals(arr1,arr2));//false???
        System.out.println(Objects.deepEquals(arr1,arr2));//true
        System.out.println(Objects.hashCode("hello"));//99162322
//        System.out.println(Objects.hashCode("helloo"));
        //多个哈希值627501671总和
        System.out.println(Objects.hash("你好","hi",12));//可变参数

    }
}

可变参数(只能在最后⼀个参数⾥⾯加)

 public static int hash(Object... values) {
 return Arrays.hashCode(values);
 }

版权声明:本文为m0_51351504原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。