Detailed analysis of the Objects class of Java's API detailed explanation

5 Objects class

5.1 Overview

tips: understand the content

Looking at the API documentation, we can see that the definition of the Objects class in the API documentation is as follows:

The package of the Objects class is under the java.util package, so it needs to be imported when using it. And the Objects class is final modified, so this class cannot be inherited.

The Objects class provides methods for some common operations on objects. For example, judging whether the objects are equal, judging whether the objects are null, and so on.

Next, let's take a look at the API documentation and look at the members of the Objects class, as follows:

We can find that there is no parameterless constructor in the Objects class, so we cannot use the new keyword to create Objects objects. At the same time, we can find that the methods provided in the Objects class are all static. So we can call these methods directly through the class name.

5.2 Common methods

tips: focus on explaining the content

Introduction to common methods

The common methods in the Objects class that we will focus on learning are as follows:

public static String toString(Object o) 					// 获取对象的字符串表现形式
public static boolean equals(Object a, Object b)			// 比较两个对象是否相等
public static boolean isNull(Object obj)					// 判断对象是否为null
public static boolean nonNull(Object obj)					// 判断对象是否不为null

The common methods in the Objects class that we'll look at are as follows:

public static <T> T requireNonNull(T obj)					// 检查对象是否不为null,如果为null直接抛出异常;如果不是null返回该对象;
public static <T> T requireNonNullElse(T obj, T defaultObj) // 检查对象是否不为null,如果不为null,返回该对象;如果为null返回defaultObj值
public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier)	// 检查对象是否不为null,如果不为null,返回该对象;如果															 // 为null,返回由Supplier所提供的值

T in the above method can be understood as an Object type.

Case presentation

Next, let's demonstrate the characteristics of these methods in the Objects class through some cases.

Case 1 : Demonstration of key learning methods

Implementation steps:

  1. Create a student class, provide two member variables (name, age); and provide the corresponding no-argument construction method and argument construction method and get/set method, and rewrite the toString method and equals method

  2. Create a test class (ObjectsDemo01), write test code in this class

As follows:

Student class

public class Student {

    private String name ;       // 姓名
    private String age ;        // 年龄

    // 其他代码略
    ...
        
}

ObjectsDemo01 test class

public class ObjectsDemo01 {

    public static void main(String[] args) {

        // 调用方法
        method_04() ;

    }

    // 测试nonNull方法
    public static void method_04() {

        // 创建一个学生对象
        Student s1 = new Student("itheima" , "14") ;

        // 调用Objects类中的nonNull方法
        boolean result = Objects.nonNull(s1);

        // 输出结果
        System.out.println(result);

    }

    // 测试isNull方法
    public static void method_03() {

        // 创建一个学生对象
        Student s1 = new Student("itheima" , "14") ;

        // 调用Objects类中的isNull方法
        boolean result = Objects.isNull(s1);

        // 输出结果
        System.out.println(result);

    }

    // 测试equals方法
    public static void method_02() {

        // 创建两个学生对象
        Student s1 = new Student("itheima" , "14") ;
        Student s2 = new Student("itheima" , "14") ;

        // 调用Objects类中的equals方法,比较两个对象是否相等
        boolean result = Objects.equals(s1, s2);     // 如果Student没有重写Object类中的equals方法,此处比较的还是对象的地址值

        // 输出结果
        System.out.println(result);

    }

    // 测试toString方法
    public static void method_01() {

        // 创建一个学生对象
        Student s1 = new Student("itheima" , "14") ;

        // 调用Objects中的toString方法,获取s1对象的字符串表现形式
        String result = Objects.toString(s1);       // 如果Student没有重写Object类中的toString方法,此处还是返回的对象的地址值

        // 输出结果
        System.out.println(result);

    }

}

Case 2 : Demonstrating need-to-know methods

public class ObjectsDemo02 {

    public static void main(String[] args) {

        // 调用方法
        method_03();

    }

    // 演示requireNonNullElseGet
    public static void method_03() {

        // 创建一个学生对象
        Student s1 = new Student("itheima" , "14") ;

        // 调用Objects对象的requireNonNullElseGet方法,该方法的第二个参数是Supplier类型的,查看源码我们发现Supplier是一个函数式接口,
        // 那么我们就可以为其传递一个Lambda表达式,而在Supplier接口中所定义的方法是无参有返回值的方法,因此具体调用所传入的Lambda表达式如下所示
        Student student = Objects.requireNonNullElseGet(s1, () -> {
            return new Student("itcast", "14");
        });

        // 输出
        System.out.println(student);

    }

    // 演示requireNonNullElse
    public static void method_02() {

        // 创建一个学生对象
        Student s1 = new Student("itheima" , "14") ;

        // 调用Objects对象的requireNonNullElse方法
        Student student = Objects.requireNonNullElse(s1, new Student("itcast", "14"));

        // 输出
        System.out.println(student);

    }

    // 演示requireNonNull
    public static void method_01() {

        // 创建一个学生对象
        Student s1 = new Student("itheima" , "14") ;

        // 调用Objects对象的requireNonNull方法
        Student student = Objects.requireNonNull(s1);

        // 输出
        System.out.println(student);

    }

}

Guess you like

Origin blog.csdn.net/qq_69748833/article/details/132571734