Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
int n = Integer.parseInt("12a");
System.out.println(n);
}
}A NumberFormatException is thrown
- Expecting
12:parseIntdoes not read the digits up to the letter and stop; the whole string must be an integer, or it throwsNumberFormatException. - Expecting a compile error: the compiler does not check what a string literal says;
parseIntchecks the text at run time.
- Which method turns the string
"2.5"into a number? - What is the difference between
Integer.parseIntandInteger.valueOf?