Java Objects-------------tool class Use

Source: Internet
Author: User

Java Objects-------------tool class Use



1, Objects Class Introduction:


Public final class Objectsextends Object

This class consists of the 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 fo R an object, and comparing the Objects.


Objects compared to the Super class object more than a letter s, which is also a Java class naming style, objects is a tool class,

Java likes to add the letter S after the tool class, such as arrays, collections, and so On.


The Objects class provides a series of practical methods for manipulating an object, and in the objects class, all methods are static

A modified, static method that can be called directly from the class Name. method name


At the same time, we usually throw nullpointerexception when we directly manipulate the ToString () of a null-referenced object, but the method of the objects class does not throw an exception, and the program outputs null or 0 (hashcode)

package java_util_objects;import java.util.objects;/** *  tests the objects class (a method that provides a series of action Objects)  *   @author  Administrator *  The English explanation below is not difficult to read, I have to be patient and stick to English documents  */public class  Objectstest {//defines a variable of type objectstest, which defaults to Nullstatic objectstest objectstest;public static  void main (string[] args)  {/* * nullpointerexception Exception thrown  */// 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 exception throws  *///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:&nbsP;* 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!")); /output this is a null object!/* *  throws a null pointer exception if the object is empty 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: *  (this method is used to check whether the given object is null, mainly used in processing functions, parameters in the constructor are Verified)  * 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) the  * string parameter is (if the object is null, throws an exception and Prompts)  * 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&nbSp;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}} 

The

Objects class also provides methods such as equals (), isNull (), and so On.

< Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >static <t> int compare (t a, t b, comparator<? Super T> c)

Returns 0 If the arguments is identical and 

static boolean deepEquals(Object a, Object b)

Returns true If the arguments is deeply equal to each other and false Otherwise.

static boolean equals(Object a, Object b)

Returns If the arguments is equal to each other and true false Otherwise.

< Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >static int hash (object... values)

Generates a hash code for a sequence of input values.

static int hashCode(Object o)

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

static Boolean < Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >isnull (object obj)

returns < Code style= "font-family: ' Dejavu Sans Mono ', monospace;padding-top:4px;margin-top:8px;line-height:1.4em;" >true  if The provided reference Is null  otherwise returns false .

static Boolean < Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >nonnull (object obj)

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

< Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >static <t> t requirenonnull (t obj)

Checks that The specified object reference is Not null .

static <t> t < Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >requirenonnull (t obj, string  Message)

Checks The specified object reference is Not null  and throws a customized nullpointerexception  if it is.

static <t> t < Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >requirenonnull (t obj, supplier< String> messagesupplier)

Checks The specified object reference is Not nullpointerexception  if it is.

static string < Code style= "font-family: ' Dejavu Sans Mono ', monospace;font-size:14px;padding-top:4px;margin-top:8px;line-height : 1.4em; " >tostring (object o)

Returns the Result of Calling tostring  for a non-null  argument and  "null"  for a null  argument.

static string toString(Object o, String nullDefault)

Returns the result of calling on the first argument if the first argument are not and toString null Returns the second Argume NT Otherwise.


This article is from the "11941149" blog, please be sure to keep this source http://11951149.blog.51cto.com/11941149/1851658

Java Objects-------------tool class Use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.