此类包含static
实用程序方法,用于操作对象或在操作前检查某些条件。 这些实用程序包括null
或null
方法,用于计算对象的哈希代码,返回对象的字符串,比较两个对象,以及检查索引或子范围值是否超出范围。
checkFromIndexSize方法
检查是否在子范围从fromIndex
(包括)到fromIndex + size
(不包括)是范围界限内0
(包括)到length
(不包括)。
如果以下任何不等式为真,则子范围被定义为超出界限:
fromIndex < 0
size < 0
fromIndex + size > length
,考虑到整数溢出
length < 0
,这是前者的不平等所暗示的
import java.util.Objects;
public class Demo1 {
public static void main(String[] args) {
//正常
String[] s = {"2","6","9","11"};
int a = Objects.checkFromIndexSize(0,3,s.length);
System.out.println(a);
System.out.println("-------------------------");
//抛异常
int[] x = {2,6,9,11};
int b = Objects.checkFromIndexSize(0,5,x.length);
System.out.println(b);
}
}
结果:
0
-------------------------
Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [0, 0 + 5) out of bounds for length 4 <3 internal calls>
at java.base/java.util.Objects.checkFromIndexSize(Objects.java:424)
at Demo1.Demo1.main(Demo1.java:14)Process finished with exit code 1
checkIndex方法
检查index
是否在0
(含)到length
(不包括)范围内。
如果以下任何不等式为真,则定义index
超出范围:
index < 0
index >= length
length < 0
,这是前者的不平等所暗示的
import java.util.Objects;
public class Demo2 {
public static void main(String[] args) {
//正常
int[] x = {1,2,3,4,5,6};
int a = Objects.checkIndex(4,x.length);
System.out.println(a);
System.out.println("-----------------");
//抛异常
int[] y = {1,2,3,4,5,6};
int b = Objects.checkIndex(6,y.length);
System.out.println(b);
}
}
结果:
0
-------------------------
Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [0, 0 + 5) out of bounds for length 4 <3 internal calls>
at java.base/java.util.Objects.checkFromIndexSize(Objects.java:424)
at Demo1.Demo1.main(Demo1.java:14)Process finished with exit code 1
deepEquals方法
返回true
如果参数是深层相等(即两个对象中的元素相等),否则false
。 两个null
值非常相等。
import java.util.Objects;
public class Demo3 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = {1,2,3,4,5,6};
int[] d = {1,2,3,4,5,};
boolean c = Objects.deepEquals(a,b);
boolean f = Objects.deepEquals(b,d);
System.out.println(c);
System.out.println(f);
}
}
结果:
true
falseProcess finished with exit code 0
equals方法
返回true
如果参数相等(即引用地址相同),彼此false
其他。 因此,如果这两个参数是null
, true
返回,如果只有一个参数为null
, false
返回。
import java.util.Objects;
public class Demo4 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
int[] b = {1,2,3,4,5,6};
boolean c = Objects.equals(a,b);
System.out.println(c);
}
}
结果:
false
Process finished with exit code 0
isNull方法
返回 true
如果提供的参考是 null
,否则返回 false
import java.util.Objects;
public class Demo5 {
public static void main(String[] args) {
int[] a = null;
int[] b = {1,2};
boolean c = Objects.isNull(a);
boolean d = Objects.isNull(b);
System.out.println(c);
System.out.println(d);
}
}
结果:
true
falseProcess finished with exit code 0
nonNull方法
返回 true
如果提供的参考是非 null
否则返回 false
。
import java.util.Objects;
public class Demo6 {
public static void main(String[] args) {
int[] a = null;
int[] b = {1,2};
boolean c = Objects.nonNull(a);
boolean d = Objects.nonNull(b);
System.out.println(c);
System.out.println(d);
}
}
结果:
false true Process finished with exit code 0
toString方法
Objects.toString(Object obj)
返回调用的结果 toString
对于非 null
参数, "null"
为 null
的说法
import java.util.Objects;
public class Demo7 {
public static void main(String[] args) {
String s = "锄禾日当午";
System.out.println(Objects.toString(s));
}
}
结果:
锄禾日当午
Process finished with exit code 0
Objects.toString(Object obj,String nullDefault)
如果第一个参数不是 null
,则返回在第一个参数上调用 toString
的结果,否则返回第二个参数。
nullDefault
- 如果第一个参数是 null
,则返回的字符串
import java.util.Objects;
public class Demo7 {
public static void main(String[] args) {
String s = null;
System.out.println(Objects.toString(s,"汗滴禾下土"));
}
}
结果:
汗滴禾下土
Process finished with exit code 0