一、序言

  今天发现了一个很有趣的问题,在群里和朋友们讨论的也比较激烈,我现在给大家阐述一下问题。

二、发现问题

  上代码。。。

package com.hzwealth.test.question;

public class IntegerTest {
public static void main(String[] args) {
//Integer
Integer a=10,b=10,c=150,d=150;
System.out.println(a==b);
System.out.println(c==d);
System.out.println(a.equals(b));
System.out.println(c.equals(d));
//String
String str=new String("AB");
String str1 ="A";
String str2 = str1+"B";
String str3="AB";
System.out.println(str==str3);
System.out.println(str==str2);
System.out.println(str2==str3);
}
}

三、解决问题

  1、Integer的问题,首先我们先看上面代码的 a==b会输出什么呢,答案是true,这个毋庸置疑,但是c==d会输出什么呢,答案是false,为什么呢?

    (1)Integer是基本数据类型的int的引用类型,官方语言叫做装箱类型,我们来看一下Integer的源码

//Integer的取值
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);
} private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
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() {}
}

  从源码我们可以看出,Integer的值为-128-127之间的时候就会自动的从Integer的缓存(IntegerCache)中去取,如果超过这个范围就会新建一个Integer的对象。

  所以a==b(a=10,b=10)为true,c==d(c=150,d=150)为false。

 2、我们来看一下String的这个问题

  (1)str==str3 为false,因为,str新建了一个对象,str3是常量池中的对象,所以这两个为false。

  (2)str2==str3,这个解释引用我群友  成都-119-EbranHan-Java 的解释:

  new 了一个 StringBuilder,然后调用了append方法,对应的就是:String str2 = str1+"B";

  这里还提到了字面量和引用,String str3="AB",这就是字面量,而引用就是,String str=new String("AB");我们自己定义的。

  字面量 jvm 优化过,直接就扔到常量池去了。

  下面是StringBuilder的toString方法,这里也是新建了一个String方法

    综上所述,str2==str3也为false。

  补充:这里的“+”不是用的String 的concat的方法,而是用的是StringBuilder来写的。而且,就算是用concat的方法也是new了一个String的对象。下面是concat的源码:

public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}

  (3)str==str2,这个也毋庸置疑的为false了,因为str是new出来的String对象,str2也是相当于new出来的一个对象。所以为false。

四、评语

  以上是我根据自己查阅资料以及和群友的交流下写出的总结,如果有不正确的地方,还望大牛指正!

Integer的比较==和String的比较==总结的更多相关文章

  1. Java中的类型转换(Integer、Long、String)

    这段时间将项目中一个模块参照C++源代码,实现一个JAVA版.主要功能是将一些字段信息转换为String类型,传输后可以进行解析. Integer.Long转为String,Java本身提供了这种转换 ...

  2. Java连载77-Integer常用方法、Integer、int、String三者相互转化、自动装箱、自动拆箱

    一.关于Integer中常用的方法 package com.bjpowernode.java_learning; ​ public class D77_1_ { public static void ...

  3. 当findById(Integer id)变成String类型

    1.原Action // 添加跳转 @RequiresPermissions("pdaManager:v_add") @RequestMapping("/pdaManag ...

  4. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  5. JAVA中Integer.valueOf, parsetInt() String.valueOf的区别和结果

    先来看段代码 public class IntegerDemo { public static void main(String[] args) { String num = null; System ...

  6. java - Integer、int 、String相互转换总结

    一下子还真记不清这三种数据类型之间的转换方法,所以做个小笔记. public class Test03 { public static void main(String[] args) { //int ...

  7. Java的Integer常量池和String常量池

    1.Integer的常量池 看下面一段代码: package cn.qlq.test; public class ArrayTest { public static void main(String[ ...

  8. Integer和int及String的总结

    秉承着总结发表是最好的记忆,我把之前遇到的问题在这里总结和大家分享一下,希望大家共同进步: 一.Integer和int首先说下自动拆装箱,基本数据类型转换为包装类型的过程叫装箱,反之则是拆箱,其中最特 ...

  9. HDU 1047 Integer Inquiry 大数相加 string解法

    本题就是大数相加,题目都不用看了. 只是注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio ...

随机推荐

  1. 身份证号码验证算法(php和js实现)

    原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21126994&id=3938244 http://www.jb ...

  2. 学习笔记45_log4net日志

    1.配置添加一个App.config *对于网站,就使用web.config ***对于App.config和web.config的配置,在表现形式上是不一致的,使用的时候应该在网上查对于的配置设置. ...

  3. jieba分词基础知识

    安装:pip install jieba 导包:import jieba 精确模式:试图将句子最精确地切开,适合文本分析(很像人类一样去分词) jieba.cut(字符串) --> 返回生成器 ...

  4. 网络安全-主动信息收集篇第二章SNMP扫描

    SNMP扫描: snmp在中大型企业中可以用来做网络管理和网络监控的使用,当开启了snmp简单网络管理后,那么客户机就可以通过这个协议向该设备发送snmp协议内容可以轻松查询到目标主机的相关信息. 以 ...

  5. 【PyTorch教程】P3. Python学习中的两大法宝函数(当然也可以用在PyTorch)

    温馨提示:为了更好的教程体验,提供视频.阅读地址 Youtube: https://www.youtube.com/playlist?list=PLgAyVnrNJ96CqYdjZ8v9YjQvCBc ...

  6. 感谢ZhangYu dalao回关

  7. 运维自动化管理服务器 CheungSSH

    CheungSSH 是一款中国人自主研发的Linux运维自动化管理服务器软件,后端使用 Python 语言+Django 的 Web 框架,前端使用 Bootstrap+Javascript+jQue ...

  8. 使用.net core中的类DispatchProxy实现AOP

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是软件开发中的一个热点,利用A ...

  9. 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。

    描述 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 0 到 n-1. ...

  10. Python 基础 常用模块

    Python 为我们提供了很多功能强大的模块,今天就主要使用的到的模块进行整理,方便后面来翻阅学习. 一.时间模块 在时间模块中我们重点介绍几种自己常用的功能,主要方便我们按照自己想要的方式获取时间 ...