以下代码显示如何实现equals()hashCode()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Point {
  private int x;
  private int y;
 
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
 
  /* implement the equals() method */
  public boolean equals(Object otherObject) {
    // Are the same?
    if (this == otherObject) {
      return true;
    }
 
    // Is otherObject a null reference?
    if (otherObject == null) {
      return false;
    }
 
    // Do they belong to the same class?
    if (this.getClass() != otherObject.getClass()) {
      return false;
    }
 
    // Get the reference of otherObject in a SmartPoint variable
    Point otherPoint = (Point) otherObject;
 
    // Do they have the same x and y co-ordinates
    boolean isSamePoint = (this.x == otherPoint.x && this.y == otherPoint.y);
 
    return isSamePoint;
  }
 
  /*
   * implement hashCode() method of the Object class, which is a requirement
   * when you implement equals() method
   */
  public int hashCode() {
    return (this.x + this.y);
  }
}
 
public class Main {
  public static void main(String[] args) {
    Point pt1 = new Point(1010);
    Point pt2 = new Point(1010);
    Point pt3 = new Point(1219);
    Point pt4 = pt1;
 
    System.out.println("pt1 == pt1: " + (pt1 == pt1));
    System.out.println("pt1.equals(pt1): " + pt1.equals(pt1));
 
    System.out.println("pt1 == pt2: " + (pt1 == pt2));
    System.out.println("pt1.equals(pt2): " + pt1.equals(pt2));
 
    System.out.println("pt1 == pt3: " + (pt1 == pt3));
    System.out.println("pt1.equals(pt3): " + pt1.equals(pt3));
 
    System.out.println("pt1 == pt4: " + (pt1 == pt4));
    System.out.println("pt1.equals(pt4): " + pt1.equals(pt4));
  }
}

上面的代码生成以下结果。

Java对象相等比较(Equals)的更多相关文章

  1. java对象比较==和equals的区别

    转载:http://blog.csdn.net/bluesky_usc/article/details/51849125 1值比较 即内容相同,我们就认为是相等的.比如:int i=5:int j = ...

  2. 【原创】关于java对象需要重写equals方法,hashcode方法,toString方法 ,compareto()方法的说明

    在项目开发中,我们都有这样的经历,就是在新增表时,会相应的增加java类,在java类中都存在常见的几个方法,包括:equals(),hashcode(),toString() ,compareto( ...

  3. java对象equals方法的重写

    根类Object中的equals方法描述: public boolean equals(Object obj)The equals method for class Object implements ...

  4. 解析Java对象的equals()和hashCode()的使用

    解析Java对象的equals()和hashCode()的使用 前言 在Java语言中,equals()和hashCode()两个函数的使用是紧密配合的,你要是自己设计其中一个,就要设计另外一个.在多 ...

  5. 为什么阿里巴巴Java开发手册中强制要求整型包装类对象值用 equals 方法比较?

    在阅读<阿里巴巴Java开发手册>时,发现有一条关于整型包装类对象之间值比较的规约,具体内容如下: 这条建议非常值得大家关注, 而且该问题在 Java 面试中十分常见. 还需要思考以下几个 ...

  6. Java基础(六)判断两个对象相等:equals、hashcode、toString方法

    1.equal方法 Object类中的equal方法用于检测一个对象是否等于另外一个对象.在Object类中,这个方法将判断两个对象是否具有相同的引用.如果两个对象具有相同的引用,它们一定是相等的.然 ...

  7. java中hashcode()和equals()的详解

    今天下午研究了半天hashcode()和equals()方法,终于有了一点点的明白,写下来与大家分享(zhaoxudong 2008.10.23晚21.36). 1. 首先equals()和hashc ...

  8. Java:对象的强、软、弱、虚引用

    转自: http://zhangjunhd.blog.51cto.com/113473/53092 1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无 ...

  9. Java:对象的强、软、弱和虚引用

    1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK ...

随机推荐

  1. 自定义实现字符串string的接口

    用char*管理String类的内存,new动态分配,在析构函数中delete char*指向的new出来的内存,一个string类需要实现那些接口可参考标准库里的string:  http://ww ...

  2. spring boot 尚桂谷学习笔记04 ---Web开始

    ------web开发------ 1.创建spring boot 应用 选中我们需要的模块 2.spring boot 已经默认将这些场景配置好了 @EnableAutoConfiguration ...

  3. box-shadow 制作单边阴影效果,不影响其它边的效果

    box-shadow 制作单边阴影效果,不影响其它边的效果:  https://blog.csdn.net/u010289111/article/details/53171128 CSS 样式实现单边 ...

  4. ES6/ES2015核心内容 import export

    ES6/ES2015核心内容:https://www.cnblogs.com/doit8791/p/5184238.html Javascript ES6学习 import export  https ...

  5. 使用api获取数据————小程序

    使用api获取数据----小程序 onLoad: function (options) { //打开页面即执行. let that = this; wx.request({ //建立链接 url: ' ...

  6. onLaunch与onLoad同步获取用户数据

    前言 在开发项目的时候遇到从全局获取用户信息,逻辑是从app.js中的onLauch获取,page页面的onLoad拿到数据填充到页面.遇到的问题是onLauch与onLoad是异步的,没办法从页面判 ...

  7. go语言从例子开始之Example17.指针

    Go 支持 指针,允许在程序中通过引用传递值或者数据结构 Example: package main import "fmt" func zeroval(ival int){ iv ...

  8. ab(http)与abs(https)压测工具

    在学习ab工具之前,我们需了解几个关于压力测试的概念 吞吐率(Requests per second) 概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请 ...

  9. (原)centos 防火墙开放端口命令

    centos 防火墙默认是关闭非系统端口的,所以用到非系统端口首先开放,命令如下 firewall-cmd --zone=public --add-port=1935/tcp --permanent ...

  10. python_suit

    全局变量 Json:双引号       Suite :ddt中不能用 suit.add(test),应该用 suit.add(loader)