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 static <T> T 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> 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> intcompare(T a, T b, Comparator<? super T> c)

Returns 0 if the arguments are identical and c.compare(a, b) otherwise.

static booleandeepEquals(Object a, Object b)

Returns true if the arguments are deeply equal to each other and false otherwise.

static booleanequals(Object a, Object b)

Returns true if the arguments are equal to each other and false otherwise.

static inthash(Object... values)

Generates a hash code for a sequence of input values.

static inthashCode(Object o)

Returns the hash code of a non-null argument and 0 for a null argument.

static booleanisNull(Object obj)

Returns true if the provided reference is null otherwise returns false.

static booleannonNull(Object obj)

Returns true if the provided reference is non-null otherwise returns false.

static <T> TrequireNonNull(T obj)

Checks that the specified object reference is not null.

static <T> TrequireNonNull(T obj, String message)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

static <T> TrequireNonNull(T obj, Supplier<String> messageSupplier)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

static StringtoString(Object o)

Returns the result of calling toString for a non-null argument and "null" for a null argument.

static StringtoString(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.