1. 程式人生 > 其它 >Java基礎封裝型別的快取

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