Java基础封装类型的缓存

类型 缓存范围
Byte -128-127
Short -128-127
Integer -128-127
Long -128-127
Character 0-127

常见笔试题

public class Main {

    public static void main(String[] args) {

        //[-128,127]
        Integer i1 = 10;
        Integer i2 = 10;
        System.out.println(i1 == i2); //true
        System.out.println(i1.equals(i2)); //true

        Integer i3 = -128;
        Integer i4 = -128;
        System.out.println(i3 == i4); //true
        System.out.println(i3.equals(i4)); //true
    }

public class Main {

    public static void main(String[] args) {

        //[-128,127]
        Integer i1 = 200;
        Integer i2 = 200;
        System.out.println(i1 == i2); //false
        System.out.println(i1.equals(i2)); //true

        Integer i3 = -128;
        Integer i4 = -128;
        System.out.println(i3 == i4); //true
        System.out.println(i3.equals(i4)); 
        //true
    }

推荐阅读:深入剖析Java中的装箱和拆箱

https://www.cnblogs.com/dolphin0520/p/3780005.html

posted @ 2022-05-11 10:57  iforeverhz  阅读(56)  评论(0编辑  收藏  举报