equals是Object类的公共方法,方法内部是用==实现的。但是很多类都重写了equals方法,例如基本数据类型的封装类和String类,重写后比较的是对象的值或者内容是否相同。而==是比较地址,但是基本数据类型,==比较的是两个变量的值是否相同,对于两个引用数据类型而言,==比较的是它们的地址是否相同。

equals方法比较内容

public class CSDN {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
System.out.println("String:" + s1.equals(s2) + "," + s1.equals(s3)); Byte a1 = new Byte((byte)1);
Byte b1 = new Byte((byte)1);
System.out.println("Byte:" + a1.equals(b1)); Short a2 = new Short((short)1);
Short b2 = new Short((short)1);
System.out.println("Short:" + a2.equals(b2)); Integer a3 = new Integer(1);
Integer b3 = new Integer(1);
System.out.println("Integer:" + a3.equals(b3)); Long a4 = new Long(1L);
Long b4 = new Long(1L);
System.out.println("Long:" + a4.equals(b4)); Float a5 = new Float(1.0f);
Float b5 = new Float(1.0f);
System.out.println("Float:" + a5.equals(b5)); Double a6 = new Double(1.0d);
Double b6 = new Double(1.0d);
System.out.println("Double:" + a6.equals(b6)); Boolean a7 = new Boolean(false);
Boolean b7 = new Boolean(false);
System.out.println("Boolean:" + a7.equals(b7)); Character a8 = new Character('1');
Character b8 = new Character('1');
System.out.println("Character:" + a8.equals(b8));
}
} 输出:
String:true,true
Byte:true
Short:true
Integer:true
Long:true
Float:true
Double:true
Boolean:true
Character:true

==比较地址

1. 对于基本数据类型是比较它们的值,下面用int类型举例演示,其他基本数据类型一样的结果。

public class CSDN {
public static void main(String[] args) {
int a = 1;
int b = 1;
System.out.println(a == b);
}
} 输出:
true

2. 对于引用数据类型,比较的是对象的地址是否相同。由于java的常量池机制,相同的字符串常量地址是一样的。

public class CSDN {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println("String:" + (s1==s2) + "," + (s1==s3) + "," + (s3==s4)); Byte a1 = new Byte((byte)1);
Byte b1 = new Byte((byte)1);
System.out.println("Byte:" + (a1==b1)); Short a2 = new Short((short)1);
Short b2 = new Short((short)1);
System.out.println("Short:" + (a2==b2)); Integer a3 = new Integer(1);
Integer b3 = new Integer(1);
System.out.println("Integer:" + (a3==b3)); Long a4 = new Long(1L);
Long b4 = new Long(1L);
System.out.println("Long:" + (a4==b4)); Float a5 = new Float(1.0f);
Float b5 = new Float(1.0f);
System.out.println("Float:" + (a5==b5)); Double a6 = new Double(1.0d);
Double b6 = new Double(1.0d);
System.out.println("Double:" + (a6==b6)); Boolean a7 = new Boolean(false);
Boolean b7 = new Boolean(false);
System.out.println("Boolean:" + (a7==b7)); Character a8 = new Character('1');
Character b8 = new Character('1');
System.out.println("Character:" + (a8==b8)); }
} 输出:
String:true,false,false
Byte:false
Short:false
Integer:false
Long:false
Float:false
Double:false
Boolean:false
Character:false

解释下第一行输出的结果:由于java的常量池机制,s1和s2它们在常量池的地址是一样的。而s3是new出来的,需要在堆内存开辟空间,地址当然和s1,s2的不一样。

下面看下几个拆箱装箱的例子

public class CSDN {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
int c = 100;
Integer d = new Integer(100); System.out.println("a == b :" + (a == b)); //a和b装箱,然后比较地址。
System.out.println("a.equals(b) :" + a.equals(b)); //a和b装箱,然后比较对象内容
System.out.println("a == c :" + (a == c)); // a先拆箱,然后再和c比较值
System.out.println("a.equals(c) :" + a.equals(c)); //c先装箱,然后比较对象内容
System.out.println("a == d :" + (a == d)); //d属于new出来的,地址和a肯定不一样
System.out.println("a.equals(d) :" + a.equals(d)); //a装箱,然后和d比较内容
System.out.println("d == c :" + (d == c)); //d拆箱,然后和c比较值
System.out.println("d.equals(c) :" + d.equals(c)); //c装箱,然后和d比较内容 }
} 输出:
a == b :true
a.equals(b) :true
a == c :true
a.equals(c) :true
a == d :false
a.equals(d) :true
d == c :true
d.equals(c) :true

简单来说,装箱就是将基本数据类型转换为包装器类型;拆箱就是将包装器类型转换为基本数据类型。

例如:

public class CSDN {
public static void main(String[] args) {
Integer a1 = 100; //自动装箱,相当于执行Integer a1 = Integer.valueOf(100);
int b1 = a1; //自动拆箱,相当于执行int b1 = a1.intValue();
}
}

其中Integer.valueOf函数源码如下:

public static Integer valueOf(int i) {
if (i >= -128 && i <= 127)
return IntegerCache.cache[i + 128];
return new Integer(i);
}

而cache[]源码如下:数组中保存的是256个Integer对象。

static final Integer cache[] = new Integer[256];

也就是说,只要在自动装箱的过程中,值在-128到127之间,那么它们==的地址都是一样的。例如最上面的例子中下面这行代码:

System.out.println("a == b :" + (a == b)); //a和b装箱,然后比较地址。

如果值超过这个范围,那么在自动装箱时就会再开辟空间,地址就会不一样。例如:

public class CSDN {
public static void main(String[] args) {
Integer a = 128;
Integer b = 128;
System.out.println(a == b);
System.out.println(a.equals(b));
}
} 输出:
false
true

如果不理解就看看源码!

java中的equals与==的区别的更多相关文章

  1. Java中“==”与equals方法的区别

    1. 用“==”比较两个变量,如果两个变量是基本类型变量,且都是数值类,则值相等就返回true 如果两个变量是引用型变量,则两个对象的地址一样,即指向同一个对象,则返回true 2.equals:St ...

  2. java中“==”和equals方法的区别,再加上特殊的String引用类型

    ==和equals的区别: 1.==是运算符,而equals是基类Object定义的一个方法,并且equals使用==定义的 2.进行比较时,分为  基本数据类型  的比较和  引用数据类型 的比较 ...

  3. Java中的equals和==的区别以及几个常用的object中的方法简单的调试方法

    一.equals 1.equals:是Object类中的方法,只能判断引用类型 2.默认判断的是地址是否相等(判断两个参数是否是同一个对象),子类中往往重写该方法,用于判断内容(值)是否相等 二.== ...

  4. java中 ==与equals 有什么区别?

    1.==既可以比较基本类型变量,又可比较引用类型变量,而equals只能比较引用类型变量: 2.equals方法支持重写,如果未重写equals方法,则比较引用变量时与==都是比较变量所指向的对象地址 ...

  5. Java中"String.equals()“和"=="的区别

    Do NOT use the `==`` operator to test whether two strings are equal! It only determines whether or n ...

  6. Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例(转)

    Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例  原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ...

  7. 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别

    Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...

  8. 【转】彻底弄懂Java中的equals()方法以及与"=="的区别

    彻底弄懂Java中的equals()方法以及与"=="的区别 一.问题描述:今天在用Java实现需求的时候,发现equals()和“==”的功能傻傻分不清,导致结果产生巨大的偏差. ...

  9. 浅谈Java中的equals和==(转)

    浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...

随机推荐

  1. Codeforces Round #497 (Div. 2) A. Romaji

    Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...

  2. 543 Diameter of Binary Tree 二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2 ...

  3. [已读]了不起的Node.js

    2015/1/22 昨天下班前看完了这本,也不算看完,redis与mysql部分没有去翻,觉得暂时用不上. 觉得第一部分的内容还不错. 第二部分主要讲fs,tcp和http这三个模块. 第三个部分是例 ...

  4. css3のborder-radius

    css3のborder-radius 今天主要练习了一下border-radius这个属性,这个是最常用的属性,所以先从它开始学习和总结吧. 我觉得需要注意以下几点: 1.书写规范: -webkit- ...

  5. Java基础之面向对象

    面向对象 1.面向对象思想:     (1)概述:面向对象是相对于面向过程而言的,面向过程强调的是功能,面向对象强调的是将功能封装进对象,强调具备功能的对象:     (2)思想特点:        ...

  6. [BZOJ2982]combination Lucas定理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982 $C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\ ...

  7. IOS之UIAlert​Controller

    你知道 UIAlertView.UIActionSheet (以及它们各自的 delegate protocols) 在 iOS 8 中已经被废弃了吗? 这是真的.在你的代码中按住 ⌘ 点击 UIAl ...

  8. 企业CIO、CTO必读的34个经典故事

    一. 用人之道 去过庙的人都知道,一进庙门,首先是弥陀佛,笑脸迎客,而在他的北面,则是黑口黑脸的韦陀.但相传在很久以前,他们并不在同一个庙里,而是分别掌管不同的庙.弥乐佛热情快乐,所以来的人非常多,但 ...

  9. sqlserver 视图用 case when

    视图用 case when 需要 用如下格式,[需要的列名]= case when...,而表里面的case 不用这样 [isNormal]=CASE WHENdbo.c_bdm_head.I_E_F ...

  10. ZOJ 3466 The Hive II (插头DP,变形)

    题意:有一个n*8的蜂房(6边形的格子),其中部分是障碍格子,其他是有蜂蜜的格子,每次必须走1个圈取走其中的蜂蜜,在每个格子只走1次,且所有蜂蜜必须取走,有多少种取法? 思路: 以前涉及的只是n*m的 ...