zoukankan      html  css  js  c++  java
  • Integer比较大小的正确方法

    一、Integer的大小比较

    package com.tsing0520;
    
    import org.junit.Test;
    
    public class IntegerTest {
    
        @Test
        public void testEquals1(){
            Integer num1 = 10 ;
            Integer num2 = 10 ;
            boolean flag = num1 == num2;
            // true
            System.out.println(flag);
        }
    
        @Test
        public void testEquals2(){
            Integer num1 = 1000 ;
            Integer num2 = 1000 ;
            boolean flag = num1 == num2;
            // false
            System.out.println(flag);
        }
    
        @Test
        public void testEquals3(){
            Integer num1 = 10 ;
            Integer num2 = 10 ;
            boolean flag = num1.equals(num2);
            // true
            System.out.println(flag);
        }
    
        @Test
        public void testEquals4(){
            Integer num1 = 1000 ;
            Integer num2 = 1000 ;
            boolean flag = num1.equals(num2);
            // false
            System.out.println(flag);
        }
    }
    
    

    二、Integer的部分源码

    package java.lang;
    
    public final class Integer extends Number implements Comparable<Integer> {    
        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
        
        private static class IntegerCache {
            static final int low = -128;
            static final int high;
            static final Integer cache[];
            static {
                // high value may be configured by property
                int h = 127;
                // ......
            }
            private IntegerCache() {}
        }
        // ......
    }
    
  • 相关阅读:
    分层应用——怎样实现登录?
    浅谈UML的概念和模型之UML九种图
    C++ 顶层 const
    Cooley-Tukey算法 (蝶形算法)
    Android 4.4 Kitkat 使能 USB adb 功能
    Linux多线程编程小结
    排序算法汇总总结
    nodeJs基础
    MyBatis入门学习(一)
    IOC/DI的基本思想
  • 原文地址:https://www.cnblogs.com/tsing0520/p/12717456.html
Copyright © 2011-2022 走看看