Integer == Integer 数字比大小随笔

说下背景吧,今天pm说有个bug,有关于权限校验,但是追到最后,发现是这里:

/**
     * 判断是否有库房位置相关的权限
     *
     * @param account
     * @param corporteKey
     * @param addressId
     * @return
     */
    public boolean checkAddressPermission(String account, String corporteKey, Integer addressId) {
        List<Integer> addressList = queryAddressList(account, corporteKey);
        if (CollectionUtils.isEmpty(addressList)) {
            return true;
        }
        for (Integer id : addressList) {
            if (id == addressId) {
                return true;
            }
        }
        return false;
    }

这块应该就是拿出角色中的数据权限来跟实际接收库房id来对比,但是这里这个 id == addressId 亮了,因为上面是Integer。

年久失修,虽然默认了Integer不能这么比,但是还是总结了一下,算是开个头吧。 

public static void main(String[] args) {

        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(111);
        list1.add(333);

        Integer a = 1;
        Integer aa = new Integer(1);
        Integer aaa = Integer.valueOf(1);

        System.out.print("a == aa:");
        System.out.println(a == aa);
        System.out.printf("a == aaa:");
        System.out.println(a == aaa);
        System.out.printf("aa == aaa:");
        System.out.println(aa == aaa);

        Integer b = 111;
        Integer bb = new Integer(111);
        Integer bbb = Integer.valueOf(111);
        System.out.print("b == bb:");
        System.out.println(b == bb);
        System.out.print("b == bbb:");
        System.out.println(b == bbb);
        System.out.print("bb == bbb:");
        System.out.println(bb == bbb);

        Integer c = 333;
        Integer cc = new Integer(333);
        Integer ccc = Integer.valueOf(333);
        System.out.print("c == cc:");
        System.out.println(c == cc);
        System.out.printf("c == ccc:");
        System.out.println(c == ccc);
        System.out.print("cc == ccc:");
        System.out.println(cc == ccc);

        System.out.print("list1.get(0) == a:");
        System.out.println(list1.get(0) == a);
        System.out.print("list1.get(0) == aa:");
        System.out.println(list1.get(0) == aa);
        System.out.print("list1.get(0) == aaa:");
        System.out.println(list1.get(0) == aaa);
        System.out.print("list1.get(2) == c:");
        System.out.println(list1.get(2) == c);
        System.out.print("list1.get(2) == cc:");
        System.out.println(list1.get(2) == cc);
        System.out.print("list1.get(2) == ccc:");
        System.out.println(list1.get(2) == ccc);

        System.out.print("list1.get(2).equals(ccc):");
        System.out.println(list1.get(2).equals(ccc));
        System.out.print("list1.get(2).intValue() == ccc.intValue():");
        System.out.println(list1.get(2).intValue() == ccc.intValue());

        int i = 222;
        int y = 222;
        System.out.println(i == y);

    }
a == aa:false
a == aaa:true
aa == aaa:false
b == bb:false
b == bbb:true
bb == bbb:false
c == cc:false
c == ccc:false
cc == ccc:false
list1.get(0) == a:true
list1.get(0) == aa:false
list1.get(0) == aaa:true
list1.get(2) == c:false
list1.get(2) == cc:false
list1.get(2) == ccc:false
list1.get(2).equals(ccc):true
list1.get(2).intValue() == ccc.intValue():true
true

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页