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 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  intComparator super T> c)

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

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

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

static intObject... values)

Generates a hash code for a sequence of input values.

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

static booleanReturns true if the provided reference is null otherwise returns false.

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

static  TChecks that the specified object reference is not null.

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

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

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

static StringReturns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

java objects_Java Objects-------------工具类使用相关推荐

  1. 【Java】java.util.Objects 工具类方法研究

    [Java]java.util.Objects 工具类方法研究 Objects 与 Object 区别 Object 是 Java 中所有类的基类,位于java.lang包. Objects 是 Ob ...

  2. Java封装OkHttp3工具类

    点击关注公众号,Java干货及时送达  作者:如漩涡 https://blog.csdn.net/m0_37701381 Java封装OkHttp3工具类,适用于Java后端开发者 说实在话,用过挺多 ...

  3. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  4. java轻量级并行工具类_16 个超级实用的 Java 工具类

    原标题:16 个超级实用的 Java 工具类 源 /juejin 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名, ...

  5. java时间日期工具类_java日期处理工具类

    java日期处理工具类 import java.text.DecimalFormat; import java.text.ParsePosition; import java.text.SimpleD ...

  6. java 日期处理工具类_Java日期处理工具类DateUtils详解

    本文实例为大家分享了Java日期处理工具类DateUtils的具体代码,供大家参考,具体内容如下 import java.sql.Timestamp; import java.text.ParseEx ...

  7. java downloadfile_FileDownload.java:文件下载工具类

    FileDownload.java:文件下载工具类 2017-04-25·Mr.Xia 4489 次浏览 文件下载功能的工具类 JavaWeb工具类目录 [http://baike.xsoftlab. ...

  8. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...

  9. java基于HuTool工具类ExcelWriter合并单元格

    ** java基于HuTool工具类ExcelWriter合并单元格 ** 1.基于HuTool工具类ExcelWriter合并单元格并且使用 jdk1.8 lambda表达式 效果如下: 用姓名和编 ...

最新文章

  1. 什么是物联网网关?物联网网关具备什么功能?_转
  2. 基于java的InputStream.read(byte[] b,int off,int len)算法学习!
  3. [html] marquee详解
  4. MySQL学习笔记06【多表查询、子查询、多表查询练习】
  5. 视窗宽高offset、client、scroll
  6. Java,想说爱你真不容易
  7. git add remote_git命令
  8. KMPBoyer-Moore
  9. NovacoBridge 软件在电子海图更新中的应用
  10. Java学习年度总结
  11. 针式 PKM 个人知识管理软件 视频简介
  12. 软件工程计算机水平 推荐表,软件工程就业推荐表2014届.doc
  13. 文章收录技巧(怎么提升网站伪原创文章的收录)
  14. 1.12 DICOM彩色图像
  15. 赵小楼《天道》深度解析(75)客观是对现有事实的认可,嘴上认可可不行,得心里认,否则就是自欺
  16. Spark入门-什么是Spark
  17. MPLAB常见问题及解决方法
  18. JavaScript倒计时算法(计算剩余多少天)实现
  19. https 加密、http2.0、keep-alive
  20. 常用的五种Python解释器

热门文章

  1. Lucene入门与使用(一) [转]
  2. UVA665 LA5658 False coin【暴力】
  3. UVA11349 Symmetric Matrix【数学】
  4. UVA11645 Bits【位运算+大数】
  5. Spark 原理 —— 从 akka 到 spark 集群的启动
  6. 歌词 —— 那些花儿
  7. matlab 高级函数 —— circshift、squeeze
  8. 矩阵分析相关证明(一) —— 正交与投影
  9. 贝叶斯分析——从数值积分到MCMC
  10. java threadlocal 并发_Java并发编程:ThreadLocal