【问题标题】:typecasting internal process in javajava中的类型转换内部过程
【发布时间】:2013-03-31 20:39:39
【问题描述】:

当我使用时

   String s = "12";      
   int n = Integer.parseInt(s);

这将 n 的值设为 12 。这对我来说很好。

它在内部做什么。这里的内部流程是什么 跑步。有人可以解释一下吗?字符串如何真正转换为 整数。在投反对票之前,请给我理由。我会纠正 下次我的错误。我搜索了这个问题。但是我没有 找到答案。

提前致谢。

【问题讨论】:

标签: java


【解决方案1】:

我建议将源代码附加到您喜欢的 IDE 中,当您需要某些东西时,您只需要跳转到任何库的源代码即可。 最好的方法是使用 IntelliJ IDEA 并创建空的 Maven 模块。然后在进入方法实现之后,您将被要求下载源代码......快速而简单。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      在这篇文章中:how it's working 它很好地展示了它是如何工作的。看完应该能正确理解

      【讨论】:

        【解决方案4】:

        您好,这是 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;
            }
            }
        

        【讨论】:

        • 我不明白它为什么会宕机。这是 parseInt 方法的实际实现,演示了它是如何工作的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 2020-10-17
        • 2011-05-08
        • 2018-09-23
        • 2015-06-27
        • 2021-11-17
        相关资源
        最近更新 更多