Integer和int比较

在Java中,当Integer的数值在-128到127之间时,使用'=='或'equals'与int比较都返回true。超出该范围,Integer与Integer间用'=='比较返回false,而用'equals'比较仍然为true。
摘要由CSDN通过智能技术生成
  •  -128~127的范围,int和Integer用"=="或"equals"比较相同数值时,都是true;

  • <-128或>127情况:

  1. Integer和int用"=="或"equals"比较相同数值时,都是true;
  2. Integer和Integer用"equals"比较相同数值时,都是true;
  3. Integer和Integer用"=="比较相同数值时,都是false;
/**
 * @author: alen.wang
 * @date: 2020-09-03 11:20
 */
public class Test {
    public static void main(String[] args) {
        Integer itr1 = 127;
        Integer itr11 = 127;
        Integer itr2 = 128;
        Integer itr3 = 128;
        Integer itr4 = -128;
        Integer itr44 = -128;
        Integer itr5 = -129;
        Integer itr6 = -129;
        int i1 = 127;
        int i2 = 128;
        int i3 = 128;
        int i4 = -128;
        int i5 = -129;

        System.out.println(&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值