Integer使用==比较的问题

  • new一个对象

    public Integer(int value) {
    this.value = value;
    }
  • 自动装箱

    public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
    }
  • 自动拆箱

    public int intValue() {
    return value;
    }

    总结:

    • int 和 int 比较,比较的是字面量的值,使用==始终是true
    • int 和 integer 比较,由于 integer 会发生自动拆箱,也是true
    • integer 和 integer 比较:
      • 若两个都是new出来的对象,则始终是false
      • 若一个是new,一个是非new(包括字面量 || Integer.valueOf( )等),那么一个是自动装箱的对象,一个是new出来的对象,始终flase
      • 两个都不是new出来的,都会发生自动装箱,就需要看值的范围,在-128-127的范围内,会获取IntegerCache里的对象,这样就是true,范围外的还是false

Integer使用==比较的问题的更多相关文章

  1. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  2. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  3. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  4. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  10. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

随机推荐

  1. 使用mysqldump备份与还原的mysql数据库

    使用mysqldump备份与还原的mysql数据库 一.mysqldump命令介绍 1.mysqldump -help 查看命令介绍: mysqldump --help 2.mysqldump登录选项 ...

  2. 全网最全的linux上docker安装oracle的详细文档,遇到了n个问题,查了几十篇文章,最终汇总版,再有解决不了的,私聊我,我帮你解决

    目录 全网最全的linux上docker安装oracle的详细文档,遇到了n个问题,查了几十篇文章,最终汇总版,再有解决不了的,私聊我,我帮你解决 1. 拉取阿里镜像oracle 2. 创建初始化数据 ...

  3. 开发一个最简单的iOS App

    开发一个最简单的iOS App 大家好,我是孜孜不倦学习的Zhangbeihai. 上月底我组织了[组队学习]TensorFlow 入门课程(中文) ,截至目前有300多同学加入.主要就是 Tenso ...

  4. jQuery基本使用

    目录 一:jQuery查找标签 1.基本选择器 二:分组与嵌套 三:组合选择器 四:jQuery基本筛选器 五:属性选择器 1.属性标签 六:JQuery表单筛选器 1.type属性 2.表单对象属性 ...

  5. Karmada多云多集群生产实践专场圆满落幕

    摘要:CNCF Karmada社区Cloud Native Days China 2022南京站成功举办. 本文分享自华为云社区<Karmada多云多集群生产实践专场圆满落幕|Cloud Nat ...

  6. Chaos 测试下的若干 NebulaGraph Raft 问题分析

    Raft 是一种广泛使用的分布式共识算法.NebulaGraph 底层采用 Raft 算法实现 metad 和 storaged 的分布式功能.Raft 算法使 NebulaGraph 中的 meta ...

  7. python 定时发送邮件

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart f ...

  8. 丧心病狂,竟有Thread.sleep(0)这种神仙写法?

    前言 最近在网上看到了一段代码,让我感到很迷茫.他在代码中使用了 Thread.sleep(0),让线程休眠时间为0秒,具体代码如下. int i = 0; while (i<10000000) ...

  9. [深度学习] Contractive Autoencoder

    ​  转载于DeepLearning: Contractive Autoencoder - dupuleng - 博客园 一.雅克比矩阵 雅克比矩阵是一阶偏导,假设(x1,x2,....,xn)到(y ...

  10. [数据分析与可视化] 数据绘图要点2-Y轴的开始与结束

    数据绘图要点2-Y轴的开始与结束 切割或不切割Y轴可能是数据可视化中最具争议的话题之一.基本上,主要问题在于 Y 轴是否应始终从零开始.数据可视化的目的是讲述一个故事,图形表达方式会对可视化讲述的故事 ...