java中的类型转换内部进程

java中的类型转换内部进程,java,Java,当我使用 String s = "12"; int n = Integer.parseInt(s); 这将n的值设为12。这对我来说很好 它在内部做什么。这里的内部流程是什么 跑步有人能解释一下吗?字符串如何真正转换为 整数。在投反对票之前,请告诉我原因。我会改正的 下次是我的错。我寻找这个问题。但我没有 找到答案 提前感谢。查看Integer.parseInt()的内部代码,例如第444行 但是,如果您将Java安装中的src.zip作为源代码附加到您最喜欢的编辑器(

当我使用

   String s = "12";      
   int n = Integer.parseInt(s);
这将
n
的值设为12。这对我来说很好

它在内部做什么。这里的内部流程是什么 跑步有人能解释一下吗?字符串如何真正转换为 整数。在投反对票之前,请告诉我原因。我会改正的 下次是我的错。我寻找这个问题。但我没有 找到答案


提前感谢。

查看
Integer.parseInt()
的内部代码,例如第444行


但是,如果您将Java安装中的
src.zip
作为源代码附加到您最喜欢的编辑器(例如Eclipse)中,最简单的方法是
Integer.class
——这样您就可以得到实际的实现。

您好,这是parseInt方法的实际实现,请查看::

    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 {    /* Only got "-" */
        throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
    }
publicstaticintparseint(字符串s)抛出NumberFormatException{
返回parseInt(s,10);
}
公共静态int-parseInt(字符串s,int基数)引发NumberFormatException
{
如果(s==null){
抛出新的NumberFormatException(“null”);
}
if(基数<字符最小基数){
抛出新的NumberFormatException(“基数”+基数+
“小于字符。最小基数”);
}
if(基数>字符最大基数){
抛出新的NumberFormatException(“基数”+基数+
“大于字符的最大基数”);
}
int结果=0;
布尔负=假;
int i=0,max=s.length();
整数极限;
int multmin;
整数位数;
如果(最大值>0){
如果(s.charAt(0)='-'){
负=真;
限制=整数.MIN_值;
i++;
}否则{
limit=-Integer.MAX_值;
}
multmin=极限/基数;
如果(i1){
返回结果;
}否则{/*只得到“-”*/
抛出NumberFormatException.forInputString;
}
}否则{
返回结果;
}
}
在本文中:
很好地展示了它的工作原理。阅读后您应该能够正确理解它。我建议您将源代码附加到您喜欢的IDE上,当您需要某些东西时,只需跳转到任何库的源代码中即可。
最好的方法是使用IntelliJ IDEA并创建空的Maven模块。然后在跳转到方法实现之后,您将被要求下载源代码。。。快速简便。

可能的重复:似乎是调用静态方法“parseInt”的重复。你没有“类型转换”任何东西。谢谢你的帮助。你看到Integer.parseInt的源代码了吗?我不明白它为什么会关闭。这是parseInt方法的实际实现,演示了它是如何工作的。