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#
随机推荐
- 杭电ACM2084--数塔
http://acm.hdu.edu.cn/showproblem.php?pid=2084 这种DP是相对容易的,一个二维数组,遍历一次,计算结果,存在指定位置. 本题关键代码是: a[i-1][j ...
- Windows 命令大全
打开控制面板的方法:输入control,回车即可打开. 以下是“运行”里常见的命令: gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址 ...
- get the runing time of C++ console program.
// 获取程序运行时间.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <time.h>#include < ...
- ie8中支持 password 的 placeholder
之前写过一篇 ie8中使用placeholder 的博客,但是,该文中的 placeholder 在 type="password" 时会出现问题,不能显示文字而是密码类型的点,所 ...
- 使用JDBC从数据库中查询数据
* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...
- CSS1-CSS3 <color>颜色知识知多少?
非本人原创,原文转载自http://www.zhangxinxu.com/wordpress/2015/07/know-css1-css3-color/ by zhangxinxu from http ...
- jquery.fullCalendar官方文档翻译(一款小巧好用的日程管理日历, 可集成Google Calendar)
1. 使用方式, 引入相关js, css后, $(‘#div_name’).fullCalendar({//options}); 接受的是一个option对象 2. 普通属性 2.1. year, ...
- windows程序消息机制(Winform界面更新有关)
windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...
- C# WinForm设置TreeView选中节点
这里假定只有两级节点,多级方法类似.遍历节点,根据选中节点文本找到要选中的节点.treeView.SelectedNode = selectNode; /// <summary> /// ...
- Android布局揭秘
前言 今天把对于布局的一些理解写下来,主要内容包括控件的属性的继承关系,控件与容器的属性的关系,以及各种类的属性的使用. 控件的属性种类 通常意义上讲,我们在对一个控件进行属性赋值的时候大体上有种类型 ...