java.lang

Class Object

  • java.lang.Object

  • public class Object
    Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
Methods 
Modifier and Type Method and Description
protected Object clone()

Creates and returns a copy of this object.
boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.
protected void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
Class<?> getClass()

Returns the runtime class of this Object.
int hashCode()

Returns a hash code value for the object.
void notify()

Wakes up a single thread that is waiting on this object's monitor.
void notifyAll()

Wakes up all threads that are waiting on this object's monitor.
String toString()

Returns a string representation of the object.
void wait()

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
void wait(long timeout)

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
void wait(long timeout, int nanos)

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
1、equals()方法:用于测试某个对象是否同另一个对象相等。它在Object类中的实现是判断两个对象是否指向同一块内存区域。这种测试用处不大,因为即使内容相同的对象,内存区域也是不同的。如果想测试对象是否相等,就需要覆盖此方法,进行更有意义的比较。例如
 class Employee{
... //此例子来自《java核心技术》卷一
public boolean equals(Object otherObj){ //快速测试是否是同一个对象
if(this == otherObj) return true; //如果显式参数为null,必须返回false
if(otherObj == null) reutrn false; //如果类不匹配,就不可能相等
if(getClass() != otherObj.getClass()) return false; //现在已经知道otherObj是个非空的Employee对象
Employee other = (Employee)otherObj; //测试所有的字段是否相等
return name.equals(otherName)
&& salary == other.salary
&& hireDay.equals(other.hireDay);
}
}
对于Object类的equals()方法来说,它判断调用equals()方法的引用于传进来的引用是否一致,即这两个引用是否指向的是同一个对象。

  Object类中的equals()方法如下:

public boolean equals(Object obj)
{
return (this == obj);
}

  即Object类中的equals()方法等价于==。

  只有当继承Object的类覆写(override)了equals()方法之后,继承类实现了用equals()方法比较两个对象是否相等,才可以说equals()方法与==的不同。

约定:对于任何非空引用x,x.equals(null)应该返回为false。

  并且覆写equals()方法时,应该同时覆写hashCode()方法,反之亦然。

int hashCode()

  Returns a hash code value for the object.

  当你覆写(override)了equals()方法之后,必须也覆写hashCode()方法,反之亦然。

  这个方法返回一个整型值(hash code value),如果两个对象被equals()方法判断为相等,那么它们就应该拥有同样的hash code。

  Object类的hashCode()方法为不同的对象返回不同的值,Object类的hashCode值表示的是对象的地址。

  即,两个对象用equals()方法比较返回false,它们的hashCode可以相同也可以不同。但是,应该意识到,为两个不相等的对象产生两个不同的hashCode可以改善哈希表的性能。

Class Object的更多相关文章

  1. CoreCLR源码探索(一) Object是什么

    .Net程序员们每天都在和Object在打交道 如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" 这个答案是对的 ...

  2. JavaScript Object对象

    目录 1. 介绍:阐述 Object 对象. 2. 构造函数:介绍 Object 对象的构造函数. 3. 实例属性:介绍 Object 对象的实例属性:prototype.constructor等等. ...

  3. javascript之Object.defineProperty的奥妙

    直切主题 今天遇到一个这样的功能: 写一个函数,该函数传递两个参数,第一个参数为返回对象的总数据量,第二个参数为初始化对象的数据.如: var o = obj (4, {name: 'xu', age ...

  4. c# 基础 object ,new操作符,类型转换

    参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...

  5. APEX:对object中数据进行简单处理?

    在Salesforce中,常常要对各种数据进行处理,已满足业务逻辑.本篇文章会介绍如何实现从object获取数据,然后将取得的数据进行一系列简单处理. 第一步:SongName__c 是一个新建的ob ...

  6. 笔记:Memory Notification: Library Cache Object loaded into SGA

    笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...

  7. Selenium的PO模式(Page Object Model)[python版]

     Page Object Model 简称POM  普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...

  8. Object是什么

    Object是什么 .Net程序员们每天都在和Object在打交道如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" ...

  9. a different object with the same identifier value was already associated with the session:

    hibernate操作: 实例化两个model类,更新时会提示  a different object with the same identifier value was already assoc ...

  10. CSharpGL - Object Oriented OpenGL in C#

    Object Oriented OpenGL in C#

随机推荐

  1. 洛谷 P1886 滑动窗口

    题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...

  2. WP开发笔记——日期时间DateTime.Now函数

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  3. ASP.NET MVC 及 Areas 简单控制路由

    ASP.NET MVC中怎么去控制路由,这个想关的文章很多,我在这里就是自我总结一下,仅供参考. 1.我们新建一个项目,查看RouteConfig.cs,代码如下: public static voi ...

  4. jQuery select的操作代码

    jQuery對select的操作的实际应用代码. //改變時的事件  复制代码代码如下: $("#testSelect").change(function(){ //事件發生  j ...

  5. 2013-07-24 IT 要闻速记快想

    ### ========================= ###凡客有闹钟?从凡客的角度来讲,闹钟等工具类应用是为推广品牌和产品服务,通过工具类产品给大众一个对凡客品牌的认知.而选择推出工具类的产品 ...

  6. 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http但是Asyn ...

  7. Web Capacity Analysis Tool 压力测试工具使用笔记

    一.背景介绍 Web Capacity Analysis Tool是微软轻量级Web压力测试工具, 早先是IIS 6.0Resource Tool kit 工具包中的一个组件,现在独立出来有一个社区版 ...

  8. 通过获取客户端Json数据字符串,反序列化为实体对象的一段代码

    #region 保存候选人数据 /// <summary> /// 保存候选人数据 /// </summary> /// <param name="entity ...

  9. roscpp源码阅读

    roscpp doxgen 这只是我摘取的一些主要代码 node_handle.cpp //NodeHandle的构造函数 void NodeHandle::construct(const std:: ...

  10. java之其它命令

    java编译命令 javac: javac -d <目录> 源文件.java 指定存放生成的class文件的路径命令行下编译带包名的java源文件: javac -d . XX.java ...