== 用于判断是否为同一引用。

比如对于 String:

  • System.out.println("abc" == "abc"); // Output: true
  • System.out.println("abc" == "a"+"bc"); // Output: true
  • System.out.println("abc" == "Abc"); // Output: false
  • System.out.println("abc" != "def"); // Output: true
  • System.out.println("abc" == new String("abc")); // Output: false

equals() 函数默认下也是判断是否是同一引用。如果想对两个对象中的值进行判等,则必须重写此函数。

比如:

public class Point{
private int x;
private int y; @Override
public boolean equals(Object o) {
if (!(o instanceof Point))
return false;
Point p = (Point) o;
return p.x == x && p.y == y;
}
}

《Beginning Java 7》 - 3 - Equalty 判等的更多相关文章

  1. java 对象是可以判空的

    比如这里存xml,这里判断了一下element是否为空,来避免空指针异常,推荐用guava的optional判空

  2. 《Beginning Java 7》 - 8 - Collecting Garbage 垃圾回收

    Java 垃圾回收机制原理: Java 语言使用 garbage collector 来进行垃圾回收.它是允许在后台的代码,间或地检查没有引用的对象(unreferenced object).发现后, ...

  3. 《Beginning Java 7》 - 5 - Hash Codes 哈希码

    哈希码和 equals() 都是用来比较的. 1. 哈希码的作用是用来提高比较的效率.因为当比较的对象比较复杂时,equals() 可能很耗时,但哈希码只需要比较一个 int .哈希码常用于集 (se ...

  4. 《Beginning Java 7》 - 9 - Nested Types 嵌套类型

    嵌套类分为四种: static member class 静态成员类 nonstatic member class 非静态成员类 anonymous class 匿名类 local class 局部类 ...

  5. 《Beginning Java 7》 - 7 - abstract class 抽象类 和 interface 接口

    1. 抽象类: 为什么用抽象类: 一些 generic 的类本身并没有现实意义,所以不需要被实例化.比如动物,自然界没有动物这个物种,但却有无数的继承自动物的物种,那么动物本身可以是一个抽象类. 抽象 ...

  6. 《Beginning Java 7》 - 6 - 深入理解 String

    public final class String implements Serializable, Comparable<String>, CharSequence 所以: 1. Str ...

  7. 《Beginning Java 7》 - 4 - finalize() 手动垃圾回收

    当我们想在系统进行垃圾回收时做一些特定的工作,我们就可以重写 finalze() 函数,因为 Object 的 此函数是空的. 比如: protected void finalize() throws ...

  8. 《Beginning Java 7》 - 2 - Cloning 克隆

    Cloning 分两类:影子克隆 shallow cloning 深度克隆 deep cloning * 调用 clone() 需要 implments Cloneable.此函数为 protecte ...

  9. 《Beginning Java 7》 - 1 - Initializer 初始化器

    Initializer 分两类:class initializer 类初始化器   instance initializer 实例初始化器 1. class initializer,在编译时运行,通过 ...

随机推荐

  1. centos7 安装 rabbitmq

    主题 因为自己学习项目可能会用到rabbitmq..我又是第一次学习.以前没安装过.所以简单记录下我在centos7环境下安装rabbitmq的过程步骤,下次可以参考. 步骤 1.杂七杂八的东西 安装 ...

  2. Spring 学习记录7 初识XmlWebApplicationContext

    主题 之前Spring相关的一些类,比如Enviromnent,BenFactory都接触了一些.有一些收获.但是直接看代码很多方法都不知为什么这样写.哪里会用到.因为太底层了.不好理解..现在从高层 ...

  3. Python编辑器IDLE傻瓜入门

    转自:http://bbs.csdn.net/topics/390451617 下载python进行安装,默认自带此工具开始->程序->Python 2.*/3.*-> IDLE ( ...

  4. Kafka Zookeeper 基本命令示例

    Kafka 新建Topic bin/kafka-topics. --replication-factor --partitions --topic my-topic 查看已存在Topic列表 bin/ ...

  5. linux进阶与hadoop

    Linux进阶命令: find .  | ls --help | more  grep ll | grep 1.txt   grep -ri  BASH 1.txt   grep -ri BASH   ...

  6. 在SharePoint解决方案中使用JavaScript (2) – 模块化

    本文是在SharePoint中使用JavaScript的第二篇文章,前面的文章包括: 在SharePoint解决方案中使用JavaScript (0) 在SharePoint解决方案中使用JavaSc ...

  7. Quartz 官网翻译(转载)

    Paths中的几个重要元素 Points void CGContextMoveToPoint (    CGContextRef c,    CGFloat x,    CGFloat y ); 指定 ...

  8. Anaconda3 ubuntu18.04

    luo@luo-All-Series:~/MyFile/Anaconda3$ luo@luo-All-Series:~/MyFile/Anaconda3$ luo@luo-All-Series:~/M ...

  9. 71. Simplify Path压缩文件的绝对路径

    [抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...

  10. 572. Subtree of Another Tree 大树里包括小树

    [抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...