zoukankan      html  css  js  c++  java
  • Integer ==与Equals【原创】

    package Equals;
    
    public class IntegerEquals {
    
        public static void main(String[] args) {
            printLine(128);
            Integer a=128;
            Integer b=128;
            System.out.println(a==b);
            System.out.println(a.equals(b));
            
            printLine(127);
            a=127;
            b=127;
            System.out.println(a==b);
            System.out.println(a.equals(b));
            
            printLine(-128);
            a=-128;
            b=-128;
            System.out.println(a==b);
            System.out.println(a.equals(b));
            
            printLine(-129);
            a=-129;
            b=-129;
            System.out.println(a==b);
            System.out.println(a.equals(b));
        }
    
        private static void printLine(int flag) {
            System.out.println("========"+flag+"========");
        }
    
    }

    Output:

    ========128========
    false
    true
    ========127========
    true
    true
    ========-128========
    true
    true
    ========-129========
    false
    true

    原因:
    基于减少对象创建次数和节省内存的考虑,[-128,127]之间的数字会被缓存。

    [-128,127]这个范围取决于java.lang.Integer.IntegerCache.high参数的设置。

        private static class IntegerCache {
        private IntegerCache(){}
    
        static final Integer cache[] = new Integer[-(-128) + 127 + 1];
    
        static {
            for(int i = 0; i < cache.length; i++)
            cache[i] = new Integer(i - 128);
        }
        }
  • 相关阅读:
    CQUOJ 10819 MUH and House of Cards
    CQUOJ 9920 Ladder
    CQUOJ 9906 Little Girl and Maximum XOR
    CQUOJ 10672 Kolya and Tandem Repeat
    CQUOJ 9711 Primes on Interval
    指针试水
    Another test
    Test
    二分图匹配的重要概念以及匈牙利算法
    二分图最大匹配
  • 原文地址:https://www.cnblogs.com/softidea/p/4206771.html
Copyright © 2011-2022 走看看