1)当==两边是对象时(String,Integer...),两边比的都是地址
2)当==两边是基本类型时(int,float),两边比的都是值
3)默认equals比的是对象的地址,但是重写的话可以改变成比较值,String和Integer的equals就是重写过的

废话少说,Run!!!

package test;

public class Test {

    public static void main(String[] args) {

        System.out.println("###############对象时比的都是地址################");
TestObj t = new TestObj();
TestObj t2 = new TestObj();
System.out.println(t==t2);//false
System.out.println(t.equals(t2));//false System.out.println("############基本类型[==]比的是值###########");
int a=1;
float b=1f;
System.out.println(a==b);//true
// System.out.println(a.equals(b)); 写法错误 System.out.println("##########Integer特殊(equals重写成比较值,但new时==还是比较地址)##########");
Integer i1=0;
Integer i2=0;
System.out.println(i1==i2);//true
System.out.println(i1.equals(i2));//true
Integer i3 = new Integer(0);
Integer i4 = new Integer(0);
System.out.println(i3==i4);//false
System.out.println(i3.equals(i4));//true
System.out.println(i1==i3);//false
System.out.println(i1.equals(i3));//true
System.out.println(i4.equals(i2));//true
int i5 = 0;
System.out.println(i1.equals(i5));//true 和基本int类型比是比值
System.out.println(i1==i5);//true 和基本int类型比也是比值
System.out.println(i3.equals(i5));//true new的和基本int类型比是比值
System.out.println(i3==i5);//true new的和基本int类型比也是比值
float f6 = 0f;//等同f6=0;
System.out.println(i1.equals(f6));//false 注意结果不同说明equals的重写只是对int类型时是比值
System.out.println(i1==f6);//true
System.out.println(i3.equals(f6));//false 注意结果不同说明equals的重写只是对int类型时是比值
System.out.println(i3==f6);//true System.out.println("=======String特殊=======");
String s1="a";
String s2="a";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
String s3 = new String("a");
String s4 = new String("a");
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
System.out.println(s1==s3);
System.out.println(s1.equals(s3));
System.out.println(s4.equals(s2)); } }

对于Integer为什么只会对int作值的比较,查看源代码可见:

/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

java细节篇(==和equals的区别)的更多相关文章

  1. java中的==和equals的区别

    关于JAVA中的==和equals函数的区别 今天在研读Thinking in java 时注意到==和equals的区别,于是就通过查看JDK_API才读懂了他们的区别,于是将心得分享一下,望批评指 ...

  2. java中hashcode和equals的区别和联系

    HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键. 那么Java运行时环境是如何判断HashSet中相同对象.Ha ...

  3. 【java基础】 == 和 equals() 的区别

    ==号和equals()方法都是比较是否相等的方法,那它们有什么区别和联系呢? 首先,==号在比较基本数据类型(指的值类型)时比较的是值,而用==号比较两个对象时比较的是两个对象的地址值: int x ...

  4. 【java基础 15】java代码中“==”和equals的区别

    导读:昨夜闲来无事,和贾姑娘聊了聊java基础,然后就说到了这个"=="和equals的问题,我俩都是以前了解过,也常用这个,但是,昨天说到的时候,又乱了,什么比较地址值,什么判断 ...

  5. java提高篇(十三)-----equals()方法总结

    equals() 超类Object中有这个equals()方法,该方法主要用于比较两个对象是否相等.该方法的源码如下: public boolean equals(Object obj) { retu ...

  6. java中等于和equals的区别

    面试的时候没答上,很受打击,特写在这里. ==是判断两个变量或实例是不是指向同一个内存空间equals是判断两个变量或实例所指向的内存空间的值是不是相同 除了String和封装器,equals()和“ ...

  7. Java 面试题 == 和 equals 的区别

    int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...

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

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

  9. Java细节----method和function的区别

    面向对象的语言叫方法 面向过程的语言叫函数 在java中没有函数这么一说,只有方法一说.属于概念上的区别. 硬要说区别. Method必须依赖于Object. Function 是独立的,不需要依赖于 ...

随机推荐

  1. 使用maven搭建ssm项目配置+tomact

    一.代码自动配置:https://www.cnblogs.com/weibanggang/p/10043201.html 二.手动配置 1.创建好maven项目,在pom.xml配置文件 <!- ...

  2. 大数据(1)初始hadoop

    1.hadoop模型如下: (上图为Hadoop1.x的布局) (Hadoop2.x较Hadoop1.x,多了YARN) Hadoop框架,是一个庞大的生态系统. 或者我们可以这样理解: 可以把整个体 ...

  3. P3740 贴海报

    P3740 贴海报 很显然,这个题是让我们维护一个区间的信息 可以考虑线段树.可是这个题,正向思维可能并不可做. 所以我们考虑逆向思维. 打个比方,你是一名保洁人员.面对已经粘在墙上的,大大小小的广告 ...

  4. 在c#中using和new这两个关键字有什么意义?

    在c#中using和new这两个关键字有什么意义?答:using 引入名称空间或者使用非托管资源, new 新建实例或者隐藏基类方法

  5. 【iOS】史上最全的iOS持续集成教程 (下)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  6. Python的静态方法和类方法

    Python中使用@staticmethod这个装饰器让方法变为静态方法 一:定义 @staticmethod: 首先它是一个装饰器,被装饰的方法不需要隐含的参数,对象和对象的实例都可以调用静态方法 ...

  7. BZOJ1004: [HNOI2008]Cards(Burnside引理 背包dp)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4255  Solved: 2582[Submit][Status][Discuss] Descript ...

  8. Go单元测试与基准测试

    Go单元测试 Go单元测试框架,遵循规则整理如下: 1.文件命名规则: 含有单元测试代码的go文件必须以_test.go结尾,Go语言测试工具只认符合这个规则的文件 单元测试文件名_test.go前面 ...

  9. Linux中用户与用户组管理

    1.基础知识 Linux作为一种多用户的操作系统(服务器系统),允许多个用户同时登陆到系统上,并响应每个用户的请求. 任何需要使用操作系统的用户,都需要一个系统账号,账号分为:管理员账号与普通用户账号 ...

  10. 10.1.2 Document类型【JavaScript高级程序设计第三版】

    JavaScript 通过Document 类型表示文档.在浏览器中,document 对象是HTMLDocument(继承自Document 类型)的一个实例,表示整个HTML 页面.而且,docu ...