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. TypeScript 之 Class

    class private 和 # 的区别 前缀 private 只是TS语法,在运行时不起作用,外部能够访问,但是类型检查器会报错 class Bag { private item: any } 修 ...

  2. vivo 低代码平台【后羿】的探索与实践

    作者:vivo 互联网前端团队- Wang Ning 本文根据王宁老师在"2022 vivo开发者大会"现场演讲内容整理而成.公众号回复[2022 VDC]获取互联网技术分会场议题 ...

  3. 青少年CTF-Web-Robots

    题目信息 题目名称:Robots 题目描述:昨天十三年社团讲课,讲了Robots.txt的作用,小刚上课没有认真听课正在着急,你能不能帮帮忙? 题目难度:一颗星 解题过程 访问题目链接 浏览器里是空白 ...

  4. 为 ASPNETCORE 7 项目添加 Serilog

    本文将介绍如何为 ASP.NET Core 项目添加 Serilog. 添加 Serilog 首先,我们需要在项目中添加 Serilog 的 NuGet 包. dotnet add package S ...

  5. 精华推荐 | 【JVM深层系列】「GC底层调优系列」一文带你彻底加强夯实底层原理之GC垃圾回收技术的分析指南(GC原理透析)

    前提介绍 很多小伙伴,都跟我反馈,说自己总是对JVM这一块的学习和认识不够扎实也不够成熟,因为JVM的一些特性以及运作机制总是混淆以及不确定,导致面试和工作实战中出现了很多的纰漏和短板,解决广大小伙伴 ...

  6. 前端基础知识-js(一)个人学习记录

    待补充: https://www.ruanyifeng.com/blog/javascript/ 运行验证: https://www.jsrun.net/new 以下仅为个人理解,如有误请指正,非常感 ...

  7. 使用 NineData 高效编写 SQL

    SQL 是 Structured Query Language 的缩写,中文翻译为"结构化查询语言".它是关系型数据库的标准语言,所有的关系型数据库管理系统(RDBMS),比如 M ...

  8. Java集合 Map 集合 与 操作集合的工具类: Collections 的详细说明

    Java集合 Map 集合 与 操作集合的工具类: Collections 的详细说明 每博一文案 别把人生,输给心情 师父说:心情不是人生的全部,却能左右人生的全部. 你有没有体会到,当你心情好的时 ...

  9. C/C++内存对齐原则

    C/C++内存对齐 what && why 当用户自定义类型时(struct 或 class),编译器会自动计算该类型占用的字节数. C/C++ 为什么要内存对齐?我道行太浅,摘抄了网 ...

  10. Node.js学习笔记----day04

    认真学习,认真记录,每天都要有进步呀!!! 加油叭!!! 一.Express 原生的http在某些方面上不足以满足我们的开发需求,所以我们需要使用框架来提高我们的开发效率,框架的目的就是提高开发效率, ...