Java Objects-------------工具类使用
1、Objects类简介:
public final class Objectsextends Object
This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.
Objects相比超类Object多了一个字母s,这也是Java类命名的一个风格,Objects是一个工具类,
Java喜欢在工具类后面加上字母s,如Arrays、Collections等。
objects类提供了一系列操作一个对象Object的实用方法,在Objects类中,所有的方法都是static
修饰的,即静态的方法,可直接通过类名.方法名 进行调用
同时,一般我们直接操作一个null引用的对象的toString()时会抛出NullpointerException,但Objects类的方法不会抛出异常,程序会相应输出null或者0(hashcode)
package java_util_objects;import java.util.Objects;/** * 测试Objects类(提供了一系列操作对象的方法) * @author Administrator * 下面的英文解释其实阅读起来一点也不难,我要耐心、坚持看英文文档 */public class ObjectsTest { //定义一个ObjectsTest类型的变量,默认是为null static ObjectsTest objectsTest; public static void main(String[] args) { /* * NullPointerException异常抛出 */ //System.out.println(objectsTest.hashCode()); /* * Returns the hash code of a non-null argument and 0 for a null argument. * Parameters:o - an object * Returns:the hash code of a non-null argument and 0 for a null argument */ System.out.println(Objects.hashCode(objectsTest)); /* * NullPointerException异常抛出 */ //System.out.println(objectsTest.toString()); /* * Returns the result of calling toString for a non-null argument and "null" for a null argument. * Parameters: * o - an object * Returns:the result of calling toString for a non-null argument and "null" for a null argument */ System.out.println(Objects.toString(objectsTest)); /* * public static String toString(Object o,String nullDefault) * Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise. * Parameters:o - an object * nullDefault - string to return if the first argument is null * Returns: * the result of calling toString on the first argument if it is not null and the second argument otherwise. */ System.out.println(Objects.toString(objectsTest,"This is a null object!"));//输出This is a null object! /* * 如果对象为空,则抛出空指针异常NullPointerException * public staticT requireNonNull(T obj) * Checks that the specified object reference is not null. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below: * (此方法用于检查所给对象是否为null,主要用于处理函数里,构造器里的参数进行校验) * public Foo(Bar bar) { * this.bar = Objects.requireNonNull(bar); * } * Type Parameters: * T - the type of the reference * Parameters: * obj - the object reference to check for nullity * Returns: * obj if not null * Throws:NullPointerException - if obj is null */ //System.out.println(Objects.requireNonNull(objectsTest));//Throw Exception /* * public static T requireNonNull(T obj,String message) * String参数为(如果对象为null,则抛出异常并发出提示) * Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below: * public Foo(Bar bar, Baz baz) { * this.bar = Objects.requireNonNull(bar, "bar must not be null"); * this.baz = Objects.requireNonNull(baz, "baz must not be null"); * } * Type Parameters:T - the type of the reference * Parameters: * obj - the object reference to check for nullity * message - detail message to be used in the event that a NullPointerException is thrown * Returns:obj if not null * Throws:NullPointerException - if obj is null */ //System.out.println(Objects.requireNonNull(objectsTest, "can not be a null object!")); System.out.println(Objects.isNull(objectsTest));//return true }}
同时Objects类也提供了诸如equals(),isNull()等方法。
static <T> int | (T a, T b, <? super T> c) Returns 0 if the arguments are identical and |
static boolean | ( a, b) Returns |
static boolean | ( a, b) Returns |
static int | (... values) Generates a hash code for a sequence of input values. |
static int | ( o) Returns the hash code of a non- |
static boolean | ( obj) Returns |
static boolean | ( obj) Returns |
static <T> T | (T obj) Checks that the specified object reference is not |
static <T> T | (T obj, message) Checks that the specified object reference is not |
static <T> T | (T obj, <> messageSupplier) Checks that the specified object reference is not |
static | ( o) Returns the result of calling |
static | ( o, nullDefault) Returns the result of calling |