Objects类总结

本文介绍了Java中Objects类的一些实用方法,如checkFromIndexSize、checkIndex、deepEquals、equals、isNull、nonNull和toString。这些方法在处理对象、验证索引、比较对象和转换对象为字符串时提供帮助,并在遇到异常情况时抛出错误。
摘要由CSDN通过智能技术生成

此类包含static实用程序方法,用于操作对象或在操作前检查某些条件。 这些实用程序包括nullnull方法,用于计算对象的哈希代码,返回对象的字符串,比较两个对象,以及检查索引或子范围值是否超出范围。

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
false

Process finished with exit code 0 

equals方法

返回true如果参数相等(即引用地址相同),彼此false其他。 因此,如果这两个参数是nulltrue返回,如果只有一个参数为nullfalse返回。

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
false

Process 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴帅345

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值