Object

在Java,Object类是超级父类,是所有类的父类。

    public boolean equals(Object obj) {
return (this == obj);
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());//返回该对象实现类的“类名+@+hashcode”值
}
public final native Class<?> getClass();//返回运行时该Class对象类型,常用于反射

我们可以看到,equals方法判断的是2个对象是否是同一个对象。

这里说下,在java中 “==” 是判断两个对象是否同一,而不是判断相等。因此Object类中的equals方法也是判断两个对象是否同一。要判断两个对象是否相等,就需要重写equals()方法。

比如

    Person a = new Person("1");
Person b = new Person("1");
System.out.println(a.equals(b));
System.out.println(a.toString());
System.out.println(b.toString());
//输出
false
com.google.common.base.Person@63961c42
com.google.common.base.Person@65b54208

这段代码打印为false,为啥呢,根据Object的源码知道equls比较的是2个对象是否为同一个。但是很明显,a和b的内存地址不一致,因此二者不是同一个,所有打印false。

但是在平时我们经常用equls方法判断2个字符串是否相等

        String a = new String("hello");
String b = new String("hello");
System.out.println(a.equals(b));
System.out.println(a.toString());
System.out.println(b.toString());
//输出
true
hello
hello

实际上,a和b仍然处于不同的内存地址。但是String类复写了Object的方法,因此造成该输出结果。具体可查看String源码。

Objects

Guava的Objects类功能比较单一,这里介绍jdk的java.util.Objects。

Objects。类由一些操作对象的静态工具方法构成,这些工具方法包括了非空检查、方法的非空参数检查、比较对象的hashCode、为对象返回一个字符串表示、比较两个对象.

该类由final修饰,该类的所有方法都是静态方法,调用简单,直接在实际应用中发掘,不在赘述。

Object和Objects的更多相关文章

  1. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'

    报错: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlace ...

  2. * -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]’

    错误描述: * -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object fr ...

  3. 最终父类【根类】:Object类&Objects类

    一.java.lang.Object类 1.Object类介绍 Object类是所有类的父类.一个类都会直接或间接继承自该类: ​ 该类中提供了一些非常常用的方法! 2.toString()方法 A: ...

  4. TIJ——Chapter Two:Everything Is an Object

    If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...

  5. [Guava学习笔记]Basic Utilities: Null, 前置条件, Object方法, 排序, 异常

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3842433.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  6. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  7. JavaScript- The Good Parts Chapter 3 Objects

    Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...

  8. Objects

    Obeject Object Object representation and value representation Subobjects Polyomrphic objecets Alignm ...

  9. 【Java】Objects 源码学习

    2017-02-10 by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6387321.html    本篇概要 Objects 与 Object ...

随机推荐

  1. Java中...的作用

    Java中...的作用,代表接收若干个相同类型的参数 public void testFunction(int...arr){    //接收若干个int类型的参数     for (int i:ar ...

  2. 伪分布式Spark + Hive on Spark搭建

    Spark大数据平台有使用一段时间了,但大部分都是用于实验而搭建起来用的,搭建过Spark完全分布式,也搭建过用于测试的伪分布式.现在是写一遍随笔,记录一下曾经搭建过的环境,免得以后自己忘记了.也给和 ...

  3. 从MySQL迁移到MariaDB(CentOS)

    MySQL是世界上最流行的开源关系数据库.原来 MariaDB 的设计初衷之一就是完全兼容 MySQL,包括 API 和客户端协议,使之能轻松成为 MySQL 的代替品.MariaDB 与 MySQL ...

  4. WebSphere MQ性能调优浅谈

    导读:目前随着我们在中国的WebSphere MQ(MQSeries)用户数量越来越多,越来越多的用户开始对MQ使用时的性能优化问题提出要求,我根据日常积累的经验谈一谈在MQ性能优化方面应该考虑的因素 ...

  5. 自己动手实现MQTT协议

    写在前面 前段时间弄IoT相关的东西,系统学习了一下 MQTT 协议,在此分享出来. 本文先是对 MQTT 协议做了简单的介绍:接着是对 MQTT协议的内容做了较为全面的解读:最后使用 Python ...

  6. 普通Apache的安装与卸载

    Apache安装与卸载ctrl+F快捷查找 1.下载apache 64位解压 官网:http://httpd.apache.org/ 文件使用记事本或者sublime2.修改 打开apache目录下的 ...

  7. 2321. 【NOIP普及组T1】方程

    2321. [NOIP普及组T1]方程 时间限制: 1000 ms  空间限制: 262144 KB 题目描述

  8. Windows Server 2008 R2服务器内存使用率过高,但与任务管理器中进程占用内存和不一致

    系统环境: Windows Server 2008 R2 + Sql Server 2008 R2   问题描述: Windows Server 2008 R2系统内存占用率过大,而在任务管理器中各进 ...

  9. int string类型互转

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  10. HBase 系列(六)——HBase Java API 的基本使用

    一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...