为什么堆栈跟踪中多次调用了 parseInt() 方法?

huangapple go评论73阅读模式
英文:

Why the stack trace contains repeat call to the parseInt() method?

问题

我使用JDK 11.0编译了以下代码:

```java
import java.util.Scanner;
public class inputWithScanner {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your name and roll number");
    String name = input.next();
    Integer rollNumber = Integer.parseInt(input.next());
    input.close();
    if (name.isEmpty())
        System.out.println("oops! You didnt enter your name");
    if (rollNumber < 0)
        System.out.println("Enter a valid roll number");
    System.out.println("Hello "+name+" Roll Number   "+rollNumber);
}

并使用以下输入测试了该程序(对于rollNumber变量的混合输入是故意的):

Enter your name and roll number
Manraj 2018CSE1023

编译器引发了一个异常:NumberFormatException,堆栈跟踪如下:

Exception in thread "main" java.lang.NumberFormatException: For input string: "2018CSE1023"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.parseInt(Integer.java:770)
        at inputWithScanner.main(inputWithScanner.java:7)

问题是:
为什么会重复调用parseInt()方法?

我尝试了相同的代码,使用valueOf()方法,并使用相同的输入执行了该程序。编译器引发了相同的异常,堆栈跟踪如下:

Exception in thread "main" java.lang.NumberFormatException: For input string: "2018CSE1023"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.valueOf(Integer.java:983)
        at inputWithScanner.main(inputWithScanner.java:7)

这表明valueOf()方法调用了parseInt()方法。但为什么parseInt()调用了自身?


<details>
<summary>英文:</summary>

I compiled the following code with JDK 11.0:

import java.util.Scanner;
public class inputWithScanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name and roll number");
String name = input.next();
Integer rollNumber = Integer.parseInt(input.next());
input.close();
if (name.isEmpty())
System.out.println("oops! You didnt enter your name");
if (rollNumber < 0)
System.out.println("Enter a valid roll number");
System.out.println("Hello "+name+" Roll Number "+rollNumber);

and tested the program with the following input (mixed input for the rollNumber variable is deliberate):

Enter your name and roll number
Manraj 2018CSE1023

the compiler raises an exception: NumberFormatException with the stack trace as follows:
```Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;2018CSE1023&quot;
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.parseInt(Integer.java:770)
        at inputWithScanner.main(inputWithScanner.java:7)

The question is:
Why is there a repeated call for parseInt() method?

I tried same code with valueOf() method and executed the program with same input. The compiler raises the same exception, and stack trace is as follows:

Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;2018CSE1023&quot;
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.valueOf(Integer.java:983)
        at inputWithScanner.main(inputWithScanner.java:7)

It shows that the valueOf() method calls parseInt() method. But Why parseInt() calls itself?

答案1

得分: 8

parseInt 被定义在这个链接中:

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s, 10); // 这是当前的第 770 行
}

然后在第 652 行(在内部调用parseInt的地方),它抛出一个异常。此时堆栈跟踪中包括了这两个不同参数的parseInt方法。

简而言之,这两个是不同的方法,一个是带一个参数的,它调用了带两个参数的方法。

valueOf 的定义在这个链接中:

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

它直接调用了带两个参数的版本。

英文:

parseInt is defined as

public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10); // this is line 770 currently
}

Then at line 652 (inside that inner call to parseInt) it throws an exception. At which point both the two-arguments parseInt and the one-argument one are in the stack trace.

In short, those are two different methods, one with one arguments that calls one with two.

valueOf is defined as

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

Which calls the version with two arguments directly.

huangapple
  • 本文由 发表于 2020年8月24日 20:48:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63561379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定