探讨 Java 中 valueOf 和 parseInt 的区别

前言

在编程中,遇到类型转换,好像会经常用到 parseInt 和 valueOf,当然这里只拿 Integer 类型进行陈述,其他类型也是雷同的;
想必有读者也跟我一样,经常交叉使用这两个方法,但却不知道这两者到底有什么区别,接下来就来探究一番;

区别

Integer.parseInt(s) 的作用就是把字符串 s 解析成有符号基本类型的 int;
Integer.valueOf(s) 把字符串 s 解析成 Integer 对象类型,返回的对象可以调用 Integer 中的方法;

接下来,通过源码进行逐一解析;

parseInt

我们首先点进 parseInt() 方法中,

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s, 10);
}

可以看到,这个被我们调用的 parseInt() 方法返回了一个重载方法:

public static int parseInt(String s, int radix) throws NumberFormatException {
    if (s == null) {
        throw new NumberFormatException("null");
    } else if (radix < 2 throw new numberformatexceptionradix radix less than character.min_radix else if radix> 36) {
        throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
    } else {
        boolean negative = false;
        int i = 0;
        int len = s.length();
        int limit = -2147483647;
        if (len <= 0) {
            throw NumberFormatException.forInputString(s);
        } else {
            char firstChar = s.charAt(0);
            if (firstChar < '0') {
                if (firstChar == '-') {
                    negative = true;
                    limit = -2147483648;
                } else if (firstChar != '+') {
                    throw NumberFormatException.forInputString(s);
                }

                if (len == 1) {
                    throw NumberFormatException.forInputString(s);
                }

                ++i;
            }

            int multmin = limit / radix;

            int result;
            int digit;
            for(result = 0; i < len; result -= digit) {
                digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0 || result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }

                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
            }

            return negative ? result : -result;
        }
    }
}

1、首先看到的是,该方法传入了两个参数,parseInt(String s, int radix),这个可以根据被调用时传入的参数,return parseInt(s, 10);,盲猜一下,s 就是表示要转换成数字型的字符串,而 radix 英文是基数的意思,这里应该表示进制,即这个传入的字符串是多少进制的,那到底是不是呢,我们接着往下看;
2、这里先是对字符串 s 是否为空,以及 radix 的大小进行一个判断,不符合条件则抛出 NumberFormatException 异常,也就是数字格式化异常;

if (s == null){
    throw new NumberFormatException("null");
} else if (radix < 2 throw new numberformatexceptionradix radix less than character.min_radix else if radix> 36) {
    throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
} else {

3、接着往下,再一次对长度进行一个校验,

int len = s.length();
if (len <= 0) {
    throw NumberFormatException.forInputString(s);
} else {
    ...
}

我在这里只想到了一个能让它抛出异常的条件,

Integer.parseInt("");

运行结果:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)

4、接下来会检测第一个字符是啥,如果是 -,则将 negative 设置成 true,表示这是个负数,并且将边界 limit 设置成最小边界;
如果不是 +,则表示该字符既不是数字,不也是性质符号,因此抛出 NumberFormatException 异常;
如果字符串 s 的长度只有1,则表明这是非数字,不符合要求,也抛出 NumberFormatException 异常;
++i 是因为如果第一位是符号的话,那么在后续的循环中追加数字则直接跳过首位;

 char firstChar = s.charAt(0);
 if (firstChar < '0') {
     if (firstChar == '-') {
         negative = true;
         limit = -2147483648;
     } else if (firstChar != '+') {
         throw NumberFormatException.forInputString(s);
     }

     if (len == 1) {
         throw NumberFormatException.forInputString(s);
     }

     ++i;
 }

5、根据进制来调整边界,以防越界;

int multmin = limit / radix;

6、Character.digit() 用于将字符转为为对应进制的整数,如果该字符不是进制内的就返回-1,例如输入的字符是9,但是进制是2,那么就不符合,则会返回-1;
然后就是进行计算;

int result;
int digit;
for(result = 0; i < len; result -= digit) {
    digit = Character.digit(s.charAt(i++), radix);
    if (digit < 0 || result < multmin) {
        throw NumberFormatException.forInputString(s);
    }

    result *= radix;
    if (result < limit + digit) {
        throw NumberFormatException.forInputString(s);
    }
}

7、最后判断是否为负数完成转换;

return negative ? result : -result;

valueOf

照例查看源码:

public static Integer valueOf(String s, int radix) throws NumberFormatException {
    return parseInt(s, radix);
}

public static Integer valueOf(String s) throws NumberFormatException {
    return parseInt(s, 10);
}

@HotSpotIntrinsicCandidate
public static Integer valueOf(int i) {
    return i >= -128 && i <= Integer.IntegerCache.high ? Integer.IntegerCache.cache[i + 128] : new Integer(i);
}

可以看出 valueOf(String s, int radix) 和 valueOf(String s) 都是直接调用返回了 parseInt 方法,而 valueOf(int i) 则是一个 int 转成 Integer 的自动装箱;
接下来探究一下 IntegerCache ,可以看出这是 Integer 的成员内部类,来看源码:

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer[] cache;
    static Integer[] archivedCache;

    private IntegerCache() {
    }

    static {
        int h = 127;
        String integerCacheHighPropValue = VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        int size;
        if (integerCacheHighPropValue != null) {
            try {
                size = Integer.parseInt(integerCacheHighPropValue);
                size = Math.max(size, 127);
                h = Math.min(size, 2147483518);
            } catch (NumberFormatException var6) {
            }
        }

        high = h;
        VM.initializeFromArchive(Integer.IntegerCache.class);
        size = high - -128 + 1;
        if (archivedCache == null || size > archivedCache.length) {
            Integer[] c = new Integer[size];
            int j = -128;

            for(int k = 0; k < c.length k ck='new' integerj archivedcache='c;' cache='archivedCache;' assert high>= 127;

    }
}

整体就是初始化一个 IntegerCache.cache 数组,数组里面存储-128到127之间的数字当做是缓存,源码一开始是分析数组长度,然后给数组赋值;
总的来说,三个重构的 valueOf() 方法还是大同小异的:

Integer valueOf(int i):返回一个表示指定的 int 值的 Integer 实例;
Integer valueOf(String s):返回保存指定的 String 的值的 Integer 对象;
Integer valueOf(String s, int radix):返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的 String 中提取的值;

相关文章

c语言函数中如何给一个字符串数组赋值?

1.打开Visual C++ 6.0,新建一个C++ source file源文件;2.输入以下代码: #include "stdio.h" #include ";3.代码关键部分,见图红色部分,第一...

.ArrayList源码三:数组扩容以及元素拷贝

数组扩容以及元素拷贝 这个 ArrayList 动态性的核心机制。ArrayList 源码三:数组扩容以及元素拷贝在之前的文章中,我们已经了解到 ArrayList 底层是基于数组 elementDa...

系列专栏(六):解构赋值

ES6作为新一代JavaScript标准,已正式与广大前端开发者见面。为了让大家对ES6的诸多新特性有更深入的了解,Mozilla Web开发者博客推出了《ES6 In Depth》系列文章。CSDN...

数组结构分为一维数组二维数组一维数组赋值使用

数组结构数组是最为常见的一种数据结构,不是基本数据类型是对象,是相同类型的用一个标识符封装到一起的基本类型数据序列组成一个对象。可以用一个统一的数组名和下标来确定数组中的元素。数组包括一维数组和二维数...

第二章 数组 2.1 数组的声明、赋值及读取

第二章 数组2.1 数组的声明、赋值及读取数组,就是多个数据或变量的集合。数组分为一维数组、二维数组、三维数组......等等。通常我们使用的是一维数组和二维数组。数组是先声明,后使用。1、声明数组数...

VBA一组代码如何搞定赋值给数组arr及回填数据给...

VBA++ 题记:一剪闲云一溪月,一程山水一年华。一世浮生一刹那,一树菩提一烟霞。岁月静好,现世安稳。纵算云水漂泊,心若安宁,亦可淡若清风。希望见者与不见者都能安康。静下心,多学习有用的知识,多提高自...