package java.lang;

/**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
*
* @author unascribed
* @see java.lang.Class
* @since JDK1.0
*/
public class Object { private static native void registerNatives();
static {
registerNatives();
} public final native Class<?> getClass(); public native int hashCode(); public boolean equals(Object obj) {
return (this == obj);
} protected native Object clone() throws CloneNotSupportedException; public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
} public final native void notify(); public final native void notifyAll(); public final native void wait(long timeout) throws InterruptedException; public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
} if (nanos > 0) {
timeout++;
} wait(timeout);
} public final void wait() throws InterruptedException {
wait(0);
} protected void finalize() throws Throwable { }
}

Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来。

在Java中,只有基本类型不是对象(数组也都扩展了Object类)。

/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类是不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(20);
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
}
}

输出:

false

false

equals比较的是两个对象是否指向同一个位置

重写equals方法:

/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(21);
Person p3 = new Person(20);
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
}
}

输出:

false

false

true

/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(21);
Person p3 = new Person(20);
Demo demo = new Demo();
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println("p1 = " + p1);
System.out.println("p1.hashCode() = " + p1.hashCode());
System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode()));
}
}

输出:

false

false

true

p1 = Person@28d93b30

p1.hashCode() = 685325104

Integer.toHexString(p1.hashCode()) = 28d93b30

一般重写equals方法后都需要重写HashCode,因为相等的两个对象必须确保hashCode相等。

/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
} @Override
public int hashCode(){
return age;
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
Person p2 = new Person(21);
Person p3 = new Person(20);
Demo demo = new Demo();
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println("p1 = " + p1);
System.out.println("p1.hashCode() = " + p1.hashCode());
System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode()));
}
}

/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
} /*
@Override
public int hashCode(){
return age;
}
*/
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
System.out.println(p1);
System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode()));
}
}

重写toString方法:

/**
* Created by N3verL4nd on 2016/12/4.
* Object类:所有类的超类
* Object类似不断抽取而来,具备着所有对象都具备的共性内容。
* 常用的共性内容:
* equals toString hashCode clone
*/ class Person{
private int age; public Person(int age){
this.age = age;
} //根据Person类的年龄进行比较
@Override
public boolean equals(Object obj){
if (!(obj instanceof Person)) {
throw new ClassCastException("类型错误");
}
Person p = (Person)obj;
return (age == p.age);
} /*
@Override
public int hashCode(){
return age;
}
*/ public String toString(){
return "Person@@" + age;
}
} public class test {
public static void main(String... args) {
Person p1 = new Person(20);
System.out.println(p1);
System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode()));
}
}

输出:

Person@@20

Person@28d93b30

最新文章

  1. Rss 订阅:php动态生成xml格式的rss文件
  2. SqlServer英文单词全字匹配
  3. JavaScript Patterns 6.5 Inheritance by Copying Properties
  4. WD硬盘型号信息
  5. @synthesize vs. @dynamic
  6. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
  7. Linux ubuntu 10.10安装OpenCv
  8. Effective C++ 的55个条款
  9. MongoDB 快速入门--初级
  10. windows log
  11. mysql主从之主键冲突
  12. Cracking the coding interview
  13. C# Trim方法去除字符串两端的指定字符
  14. mysql记录所有执行过的SQL
  15. Jsonql——给RESTful API插上一对翅膀
  16. 团队作业8——第二次项目冲刺(Beta阶段)Day2--5.19
  17. UVa439——骑士的移动
  18. TIJ -- CountDownLatch
  19. CentOS7安装Jdk1.8
  20. [UE4]响应鼠标点击

热门文章

  1. selenium自动化之xpath定位*必会技能*
  2. VMware 完成 27 亿美元的 Pivotal 收购 | 云原生生态周报 Vol. 34
  3. schedule of 2016-11-7~2016-11-10(Monday~Thursday)——1st semester of 2nd Grade
  4. 比特币学习笔记(一)---在windows下编译搭建比特币环境
  5. 关于Mach-O类型文件那点事
  6. Netty快速入门(08)ByteBuf组件介绍
  7. 快速回顾MySQL:简单查询操作
  8. .NET Core 3 WPF MVVM框架 Prism系列之事件聚合器
  9. vPlayer 模块Demo
  10. cogs 1176. [郑州101中学] 月考 Map做法