下面是我看别人博客和java API总结的

首先得明确一个概念就是:

  == 的用法   ==比较对象在内存中的地址是否相等。如是是两个基本数据类型变量的比较则比较的是这两个变量值是否相等,但是这里要注意,对于Integer对象来说,其能存储的范围为(-128~127),超过范围则存储到堆内存中。,若是比较两个引用型变量则比较的是对象存储的地址是否相同,即栈中的内容是否相同,(对象存储在堆中,而对象的引用是存储在栈中)(这些必须先明确,否则后面的源码分析就不好理解)

   equals的用法    equals比较的是对象之间内容是否相同。

先看java中的Object抽象类对equals()方法的定义:

  public boolean equals(Object obj) {
    return (this == obj);
  }

这是对传入的对象进行== 比较,因为是引用类型,所以比较的是地址。所以在Object类中的equals是比较两个对象的应用地址是否相同(后面我们将要讲到的String类型的equals()方法就不是这样了,因为它重写了equals()方法!)

我们看String类中对equals()方法的定义:

public boolean equals(Object anObject) {
if (this == anObject) { // 如果是相同的对象
return true;
}
if (anObject instanceof String) { //不是相同对象就比较内容
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != ) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

所以对String的equals()方法是比较当前字符串与传进来的字符串是否相同

Java常量池

  Java的8种基本类型(Byte, Short, Integer, Long, Character, Boolean, Float, Double), 除Float和Double以外, 其它六种都实现了常量池, 但是它们只在大于等于-128并且小于等于127时才使用常量池。

由如下例子可以看出:

import org.junit.Test;

public class JUnitTest {

    @Test
public void test1() {
Integer a = 127;
Integer b = 127;
System.out.println("等于127:");
System.out.println(a == b); //true
System.out.println("*****************"); a = 128;
b = 128;
System.out.println("等于128:");
System.out.println(a == b); //false
System.out.println("*****************"); a = -128;
b = -128;
System.out.println("等于-128:");
System.out.println(a == b); //true
System.out.println("*****************"); a = -129;
b = -129;
System.out.println("等于-129:");
System.out.println(a == b); //false
System.out.println("*****************"); // 测试Boolean
System.out.println("测试Boolean");
Boolean booleanA = false;
Boolean booleanB = false;
System.out.println(booleanA==booleanB); //true System.out.println("*****************"); boolean booleanC = false;
boolean booleanD = false;
System.out.println(booleanC==booleanD); //true System.out.println("*****************");
// 测试Float
System.out.println("测试Float");
float floatA = 1.4f;
float floatB = 1.4f;
System.out.println(floatA==floatB); //true System.out.println("*****************"); Float floatC = 12.3f;
Float floatD = 12.3f;
System.out.println(floatC==floatD); //false System.out.println("*****************");
// 测试Float
System.out.println("测试Character");
Character charA = 'A';
Character charB = 'A';
System.out.println(charA==charB); //true
System.out.println("*****************");
char charC = 'B';
char charD = 'B';
System.out.println(charC==charD); //true
System.out.println("*****************"); }
}

当我们给Integer赋值时,实际上调用了Integer.valueOf(int)方法,查看源码,其实现如下:

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

而IntegerCache实现如下:

private static class IntegerCache {
static final int high;
static final Integer cache[]; static {
final int low = -128; // high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
} private IntegerCache() {}
}

注意cache数组是静态的。

java中的equals和==的更多相关文章

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

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

  2. 浅谈Java中的equals和==

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

  3. Java中的equals和hashCode方法

    本文转载自:Java中的equals和hashCode方法详解 Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要 ...

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

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

  5. java集合(3)- Java中的equals和hashCode方法详解

    参考:http://blog.csdn.net/jiangwei0910410003/article/details/22739953 Java中的equals方法和hashCode方法是Object ...

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

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

  7. Java中的equals和hashCode方法详解

    Java中的equals和hashCode方法详解  转自 https://www.cnblogs.com/crazylqy/category/655181.html 参考:http://blog.c ...

  8. 关于Java中的equals方法

    关于Java中的equals方法 欢迎转载,但是请填写本人的博客园原址https://www.cnblogs.com/JNovice/p/9347099.html 一.什么是equals方法 equa ...

  9. 沉淀再出发:java中的equals()辨析

    沉淀再出发:java中的equals()辨析 一.前言 关于java中的equals,我们可能非常奇怪,在Object中定义了这个函数,其他的很多类中都重载了它,导致了我们对于辨析其中的内涵有了混淆, ...

  10. 转:Java中的equals和hashCode方法详解

    转自:Java中的equals和hashCode方法详解 Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要重写这 ...

随机推荐

  1. Fermat vs. Pythagoras POJ - 1305 (数论之勾股数组(毕达哥拉斯三元组))

    题意:(a, b, c)为a2+b2=c2的一个解,那么求gcd(a, b, c)=1的组数,并且a<b<c<=n,和不为解中所含数字的个数,比如在n等于10时,为1, 2, 7,9 ...

  2. day16 Python map函数

    num_l=[1,2,10,5,3,7] #lambda x:x+1 # def add_one(x): # return x+1 #lambda x:x+1 # def reduce_one(x): ...

  3. Shell逻辑语句

    case esac 语句 参考 :https://blog.csdn.net/wu20093346/article/details/47210809 case ... esac 与其他语言中的 swi ...

  4. MP实战系列(三)之实体类讲解

    首先说一句,mybatis plus实在太好用了! mybaits plus的实体类: 以我博客的用户类作为讲解 package com.blog.entity; import com.baomido ...

  5. 【Codeforces 142C】Help Caretaker

    Codeforces 142 C 题意:给一个\(n\times m\)的空矩阵,求里面放最多的可旋转的\(T\)字形的个数,并输出方案. 思路1: 由于\(n\)和\(m\)比较小,所以可以尝试搜索 ...

  6. Android学习之基础知识五—编写聊天界面

    第一步:在app/build.grandle添加RecyclerView依赖库 第二步:在activity_main.xml文件中编写主界面:聊天.发送框.发送按钮三个部分 第三步:编写Message ...

  7. 18-(基础入门篇)GPRS(Air202)拨打电话--(由于板子做修订,所以暂停更新)

    https://www.cnblogs.com/yangfengwu/p/9968883.html 这个直接用官方给的demo就可以 先睹为快 现在说个需求哈,是当初一个人给提出的需求 例如存入的号码 ...

  8. Luogu2046 NOI2010 海拔 平面图、最小割、最短路

    传送门 首先一个不知道怎么证的结论:任意点的\(H\)只会是\(0\)或\(1\) 那么可以发现原题的本质就是一个最小割,左上角为\(S\),右下角为\(T\),被割开的两个部分就是\(H=0\)与\ ...

  9. LiveCharts文档-4基本绘图-3其他

    原文:LiveCharts文档-4基本绘图-3其他 4基本绘图-3其他 因为每个图表的使用方法大同小异,所以不再啰嗦重复,具体直接看这个链接里的介绍.原文链接 其他的图表类型有 基本堆叠图 基本条形图 ...

  10. BootStrap学习(7)_轮播图

    一.轮播图 Bootstrap 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除此之外,内容也是足够灵活的,可以是图像.内嵌框架.视频或者其他您想要放置的任何类型的内容. 如果 ...