Objects工具类
Objects 工具类
- jdk1.7 引进的 ⼯具类,都是静态调 ⽤的 ⽅法,jdk1.8 新增了部分 ⽅法。
重点方法
- equals:⽤于字符串和包装对象的 ⽐较,先 ⽐较内存地址,再 ⽐较值
1String str1 = "abc";
2 String str2 = "abc";
3 System.out.println(Objects.equals(str1, str2)); //true
- deepEquals:数组的 ⽐较,先 ⽐较内存地址,再 ⽐较值,如 String/char/byte/int 数组, 或者包 装类型 Integer 等数组。
1String[] arr1 = {"aa", "bb", "cc"};
2 String[] arr2 = {"aa", "bb", "cc"};
3 System.out.println(Objects.deepEquals(arr1,arr2)); //true
- hashCode:返回对象的 hashCode,若传 ⼊的为 null,返回 0
1System.out.println(Objects.hashCode("abc"));//96354
2 System.out.println(Objects.hashCode("abc"));//96354
3 System.out.println(Objects.hashCode("acb"));//96384
- hash:传 ⼊可变参数的所有值的 hashCode 的总和,底层调 ⽤ Arrays.hashCode
1System.out.println(Objects.hash("abc",12,32L));//92626389