== 能用于基本类型之间、基本类型与引用类型之间及相同引用类型之间,不能用于不同引用类型之间

对于基本类型,取值来对比,对于引用类型,取地址来对比

int a= 1;
Integer b= 1;
System.out.println(a== b);//true

Integer 自动拆箱

int a= 1;
Integer b= 1;
System.out.println(a== b);//true

基本类型之间 值得对比

int a= 1;
Double b= 1.0;
//Short b= 1;
//Long b= 1;
System.out.println(a== b);//true

自动拆箱的时机并不局限于基本类型与其对应的包装类型

Integer a= 1;
Integer b= 1;
System.out.println(a== b); //true Integer a1= 1;
Integer b1= new Integer(1);
System.out.println(a1== b1); //false

类似Integer a= 1直接赋值会被编译为Integer a= Integer.valueOf(1); 源码如下

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

对于-128到127之间的数,第一次赋值时会进行缓存,Integer a= 1时,会将1进行缓存,下次再写Integer b= 1时,就直接从缓存中取,就不会new了,使用为true

而如果是通过new Integer()的方式,不会利用缓存。

----------------------------------------------------------------------------------------------------------------

equals

首先,以Short为例,看一下其equals的源码

public boolean equals(Object obj) {
if (obj instanceof Short) {
return value == ((Short)obj).shortValue();
}
return false;
}

可知,equals()返回值为true的条件是:instanceof为true且值相等,因此:

Short a= 1;
Integer b= new Integer(1);
System.out.println(a.equals(b)); //false instanceof不为true Integer a1= 1;
Integer b1= new Integer(1);
System.out.println(a1.equals(b1)); //true

==与equals的各种情况的更多相关文章

  1. EffectiveJava(10)覆盖equals是视情况覆盖toString

    覆盖equals是视情况覆盖toString 1.toString返回字符串 className@163b91 -calssName 类的名称 @ @ 163b91 散列码的无符号十六进制表示法 2. ...

  2. java equals和==的区别

    大概说equals和==都比较的是什么: 1. boolean tem = a == b; 首先==比较的肯定是地址,从堆栈的角度说也就是说==比较的是栈上面的内容.因为栈是用来存放地址或是java中 ...

  3. 为什么重写equals时必须重写hashCode方法?

    原文地址:http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html 首先我们先来看下String类的源码:可以发现Stri ...

  4. Java hashCode() 和 equals()的若干问题

    原文:http://www.cnblogs.com/skywang12345/p/3324958.html 本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() ...

  5. Java hashCode() 和 equals()的若干问题解答

    本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() 与 == 的区别是什么? 3 hashCode() 的作用是什么? 4 hashCode() 和 equa ...

  6. 判断Set里的元素是否重复、==、equals、hashCode方法研究-代码演示

    被测试类,没有重写hasCode()和equals()方法: package niukewang; import java.util.Objects; public class setClass { ...

  7. java equals 和 "==" 比较

    java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean   他们之间的比较,应用双等号(== ...

  8. hashcode与equals

    归纳一下就是hashCode是用于查找使用的,而equals是用于比较两个对象的是否相等的.以下这段话是从别人帖子回复拷贝过来的: .hashcode是用来查找的,如果你学过数据结构就应该知道,在查找 ...

  9. java对象equals方法的重写

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

随机推荐

  1. cout顺序,i++和++i

    先看下以下代码 #include<iostream> using namespace std; ; int f1() { x = ; return x; } int f2() { x = ...

  2. 【转】防止CListCtrl闪烁的几种方法

    转载出处:http://blog.sina.com.cn/s/blog_5ee42ba30100g50j.html 1.使用SetRedraw禁止窗口重绘,操作完成后,再恢复窗口重绘 m_ctlLis ...

  3. iOS开发之--制作属于自己的frameWork

    开发的时候,有时候,我们会遇到协同开发,在协同开发的时候,每个开发者都会创建自己的工具类,还有就是当一个项目需要嵌套到另一个项目里面,这些时候,如果能把所需的部分打包成framework,会方便很多, ...

  4. ViewPager滑动引导页

    ViewPager实现Animation动画引导页   http://blog.csdn.net/ye_scofield/article/details/44831357 SurfaceView实现动 ...

  5. ubuntu设置开机启动脚本

    rc.local脚本 rc.local脚本是一个ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令.该脚本位于/etc/路径下,需要root权限才能修改. 该脚本具体格式如下: #! ...

  6. python之MySQL学习——防止SQL注入(参数化处理)

    import pymysql as ps # 打开数据库连接 db = ps.connect(host=', database='test', charset='utf8') # 创建一个游标对象 c ...

  7. Linux系统下实时监控网口速率的shell脚本

    修改后的脚本文件 #!/bin/bash #Modified by lifei4@datangmobile.cn echo ===DTmobile NetSpeedMonitor=== sleep 1 ...

  8. rest_framework之url控制器详解

    一 自定义路由(原始方式) from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^books/ ...

  9. mysql大数据查询优化

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  10. Linux学习笔记(8)文件搜索与帮助

    帮助: (1) man ls (2) info  ls  (3) whatis ls  (4) help 搜索: (1) which  ls :查看ls命令所在绝对路径 (2) locate user ...