• Integer.valueOf与Integer.parseInt的小疑惑


    参考博客:

    http://www.importnew.com/9162.html

    测试代码如下:

    public class Main {
    
        /**
         * 备注:结果跟你的JDK版本有关系:
         * 
         * 我的是java version "1.6.0_16"
         * 
         *这是JDK中的Integer.java中valueOf的源代码:
          public static Integer valueOf(String s) throws NumberFormatException {
            return Integer.valueOf(parseInt(s, 10));
          }
          
          public static Integer valueOf(int i) {
            if(i >= -128 && i <= IntegerCache.high)
                return IntegerCache.cache[i + 128];
            else
                return new Integer(i);
         }
          
         public static int parseInt(String s) throws NumberFormatException {
            return parseInt(s,10);
         }
         
            public static int parseInt(String s, int radix)
            throws NumberFormatException
        {
            if (s == null) {
                throw new NumberFormatException("null");
            }
    
        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                            " less than Character.MIN_RADIX");
        }
    
        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                            " greater than Character.MAX_RADIX");
        }
    
        int result = 0;
        boolean negative = false;
        int i = 0, max = s.length();
        int limit;
        int multmin;
        int digit;
    
        if (max > 0) {
            if (s.charAt(0) == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
            } else {
            limit = -Integer.MAX_VALUE;
            }
            multmin = limit / radix;
            if (i < max) {
            digit = Character.digit(s.charAt(i++),radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            } else {
                result = -digit;
            }
            }
            while (i < max) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++),radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        if (negative) {
            if (i > 1) {
            return result;
            } else {    
            throw NumberFormatException.forInputString(s);
            }
        } else {
            return -result;
        }
        }
         * @param args
         */
        public static void main(String[] args) {
    
            System.out.println(Integer.valueOf(127) == Integer.valueOf(127));
            System.out.println(Integer.valueOf("127") == Integer.valueOf(127));
            System.out.println(Integer.valueOf("127") == Integer.valueOf("127"));
            
            System.out.println(Integer.parseInt("127") == Integer.parseInt("127"));
            
            System.out.println(Integer.valueOf(127) == Integer.parseInt("127"));
            System.out.println(Integer.valueOf("127") == Integer.parseInt("127"));
    
            System.out.println(Integer.valueOf(128) == Integer.valueOf(128));
            System.out.println(Integer.valueOf("128") == Integer.valueOf(128));
            System.out.println(Integer.valueOf("128") == Integer.valueOf("128"));
    
            System.out.println(Integer.parseInt("128") == Integer.parseInt("128"));
            
            System.out.println(Integer.valueOf(128) == Integer.parseInt("128"));
            System.out.println(Integer.valueOf("128") == Integer.parseInt("128"));
            
            
        }
        /**
        true
        true
        true
        true
        true
        true
        false
        false
        false
        true
        true
        true
        */
    
    }
  • 相关阅读:
    Nginx安装及配置
    nginx主(子)配置文件参考
    harbor私有仓库部署
    k8s内网安装部署(二)
    k8s部署之系统初始化(一)
    redis部署安装【建议收藏】
    nginx优化【收藏篇】
    nginx之用户验证配置(实操)
    nginx反向代理和负载均衡《实战》
    nginx安装
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/3627905.html
Copyright © 2020-2023  润新知