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#
随机推荐
- storm 入门
Storm的典型用例有哪些呢? 流处理:正如前面的例子中所展示的,和其他流处理系统不同的是,使用Storm不需要中间队列. 连续计算:向客户端持续发送数据,以便它们能实时更新.显示结果,例如网站统计. ...
- ARM公布“物联网”嵌入式mbed OS系统软件平台
继ARM公司发布了为嵌入式微控制器设计的Cortex-M7架构处理器,ARM又公布了专为廉价低功耗“物联网”设计的新版软件及系统平台,以加速物联网设备的发展及部署.该软件为基于ARM现有Cortex- ...
- Jquery Slick幻灯片插件
slick 是一个基于 jQuery 的幻灯片插件,具有以下特点: 支持响应式 浏览器支持 CSS3 时,则使用 CSS3 过度/动画 支持移动设备滑动 支持桌面浏览器鼠标拖动 支持循环 支持左右控制 ...
- Java IO流详尽解析
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- “~/Views/Home/Text.aspx”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。
在MVC架构中使用aspx页面,需要在Text.aspx中开头加入如下代码: <%@ Page Language="C#" Inherits="System.Web ...
- asp.net图片上传实例
网站后台都需要有上传图片的功能,下面的例子就是实现有关图片上传. 缺点:图片上传到本服务器上,不适合大量图片上传. 第一.图片上传,代码如下: xxx.aspx 复制代码代码如下: <td cl ...
- RGB颜色二值化
原理:RGB颜色根据计算'灰度'的公式,可以转化为黑白2种颜色,实现二值化. 业务场景的应用:可以根据背景颜色,取一个黑色或白色的颜色,作为背景色上的文案字体颜色 具体代码: function get ...
- mysql手工注入
以下是mynona本人原创的,奉献给大家,不要小看数据库注入 参考: http://www.daydaydata.com/help/sql/advance/limit.html http://www. ...
- Hadoop命令摘录
一:文件操作 1.建立目录 [hadoop@hadoop1:hadoop]$bin/hadoop dfs -mkdir testdir 在HDFS中建立一个名为testdir的目录 2.上传文件到HD ...
- Python支持中文注释
三处设置,使Python的Eclipse开发环境(使用PyDev)支持中文 - (a)Eclipse的Window菜单Editors设置: Eclipse工具条 -> Window -> ...