Class Object
Class Object
- java.lang.Object
public class Object
ClassObjectis the root of the class hierarchy. Every class hasObjectas a superclass. All objects, including arrays, implement the methods of this class.
| 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. |
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()方法如下:
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的更多相关文章
- CoreCLR源码探索(一) Object是什么
.Net程序员们每天都在和Object在打交道 如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" 这个答案是对的 ...
- JavaScript Object对象
目录 1. 介绍:阐述 Object 对象. 2. 构造函数:介绍 Object 对象的构造函数. 3. 实例属性:介绍 Object 对象的实例属性:prototype.constructor等等. ...
- javascript之Object.defineProperty的奥妙
直切主题 今天遇到一个这样的功能: 写一个函数,该函数传递两个参数,第一个参数为返回对象的总数据量,第二个参数为初始化对象的数据.如: var o = obj (4, {name: 'xu', age ...
- c# 基础 object ,new操作符,类型转换
参考页面: http://www.yuanjiaocheng.net/webapi/config-webapi.html http://www.yuanjiaocheng.net/webapi/web ...
- APEX:对object中数据进行简单处理?
在Salesforce中,常常要对各种数据进行处理,已满足业务逻辑.本篇文章会介绍如何实现从object获取数据,然后将取得的数据进行一系列简单处理. 第一步:SongName__c 是一个新建的ob ...
- 笔记:Memory Notification: Library Cache Object loaded into SGA
笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...
- Selenium的PO模式(Page Object Model)[python版]
Page Object Model 简称POM 普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...
- Object是什么
Object是什么 .Net程序员们每天都在和Object在打交道如果你问一个.Net程序员什么是Object,他可能会信誓旦旦的告诉你"Object还不简单吗,就是所有类型的基类" ...
- 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 ...
- CSharpGL - Object Oriented OpenGL in C#
Object Oriented OpenGL in C#
随机推荐
- ASP.NET MVC强制返回XML
GlobalConfiguration.Configuration.Formatters.Remove(config.Formatters.JsonFormatter);
- 【风马一族_xml】xmlp之dtd1
什么是XML约束? 在xml技术里,可以编写一个文档来约束一个xml文档的写法,这称之为xml约束 2. 为什么要使用xml约束? 参看提示栏 3. xml约束的作用? 约束xml的写法 对xml进行 ...
- Spring MVC的启动过程
一.概述 下面一个基本的运用springMVC的的web.xml的配置,这里要注意两个地方,一个是ContextLoadListener,一个是DispatcherServlet.web容器正是通过这 ...
- 批量修改文件后缀(Python)
近期下载了很多各种教程, 但是不幸的是后缀名都是 ".mp4", 而本人喜欢 ".rmvb" 后缀,由于有轻微洁癖, 受不了后面的 ".mp4&quo ...
- HTTP 错误405.0 - Method Not Allowed
在使用IIS发布网页的时候,出现下图这个问题 那么出现这个问题的原因可能有很多种,但是我目前是这么解决的,下面说一下我个人解决这个问题的方案,仅供参考. 首先,以管理员的身份启动cmd 如果电脑是64 ...
- linux设备驱动模型
尽管LDD3中说对多数程序员掌握设备驱动模型不是必要的,但对于嵌入式Linux的底层程序员而言,对设备驱动模型的学习非常重要. Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统 ...
- iTween基础之Punch(摇晃)
一.基础介绍:二.基础属性 原文地址 : http://blog.csdn.net/dingkun520wy/article/details/50828042 一.基础介绍 PunchPosition ...
- 分类图 Class Diagram
类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图. 类图是系统中静态视图的一部分,静态视图可以包括许多的类图. 静态视图用于为软件系统进行结构建模,它构造系统的词汇和关系,而 ...
- Android应用市场提交入口
应用市场是整个移动生态系统的核心,然而对于中国用户来说,Google Play应用商店却因为种种原因,在中国一直无法长期稳定的运作,又加上Android系统的开源特性,从而在中国造就出大量的第三方应用 ...
- res/drawable目录下图片的Uri
http://liuyun025.iteye.com/blog/1280838 有时候,我们要用到res/drawable目录下的图片Uri,而这个Uri该如何生存呢?下面就是这Uri的生成方法: U ...