Quick revision
The short answer
Integer.parseInt(" 42") throws NumberFormatException because parseInt does not trim spaces, while "+42" returns 42 because a leading sign is allowed. "2147483648" throws because it is one more than Integer.MAX_VALUE, parseInt("ff", 16) returns 255, and Integer.toBinaryString(-1) returns 32 ones because it prints the two's complement bits as an unsigned number. Long.parseLong("42L") throws because the L suffix exists only in source code, and Integer.valueOf("042") returns 42, because parsing uses radix 10 and only Integer.decode treats a leading zero as octal.
What to remember
In this answer, we saw that parseInt is strict: it does not trim spaces, accepts one leading sign, and throws instead of overflowing, so " 42" and "2147483648" fail while "+42" is 42. The radix argument picks the base, so "ff" in base 16 is 255, and toBinaryString shows negative numbers as their unsigned two's complement bits, 32 ones for -1. Finally, source literal rules do not apply to parsing: "42L" fails and "042" is 42, and only Integer.decode reads leading zeros as octal.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Integer.parseInt(" 42") throws NumberFormatException because parseInt does not trim spaces, while "+42" returns 42 because a leading sign is allowed. "2147483648" throws because it is one more than Integer.MAX_VALUE, parseInt("ff", 16) returns 255, and Integer.toBinaryString(-1) returns 32 ones because it prints the two's complement bits as an unsigned number. Long.parseLong("42L") throws because the L suffix exists only in source code, and Integer.valueOf("042") returns 42, because parsing uses radix 10 and only Integer.decode treats a leading zero as octal.
Why this question is asked
Parsing numbers from text happens everywhere: request parameters, CSV files, environment variables, message headers. Each call in this question shows one rule of Integer.parseInt that people assume differently, and each of those assumptions has caused real incidents, from rejected input with a stray space to ids that overflow. Interviewers use this question to check that we know the exact contract of the methods we call every day. In this answer, we will recap what parsing with a radix means, go through the whitespace and sign rules, the range check, other bases and binary output, and finally the difference between literal syntax and parsing, which is where the L suffix and the leading zero come in.
1A Quick Recap of parseInt and Radix
Integer.parseInt(String s) reads text and returns an int. Its contract is strict: the string must contain only digits, optionally preceded by one plus or minus sign, and the result must fit in the int range from -2,147,483,648 to 2,147,483,647. Anything else throws NumberFormatException, an unchecked exception whose message shows the input.
The radix is the number base used to read the digits. parseInt(s) uses radix 10. parseInt(s, radix) accepts any radix from 2 to 36, where the letters a to z (in either case) stand for digit values 10 to 35. Integer.valueOf works the same way but returns an Integer object instead of an int, using the small Integer cache for values from -128 to 127.
| Call | Result | Reason |
|---|---|---|
parseInt(" 42") | NumberFormatException | spaces are not trimmed |
parseInt("+42") | 42 | one leading + or - is allowed |
parseInt("2147483648") | NumberFormatException | MAX_VALUE + 1 does not fit in int |
parseInt("ff", 16) | 255 | f is 15 in base 16: 15 x 16 + 15 |
toBinaryString(-1) | 32 ones | two's complement bits shown as unsigned |
parseLong("42L") | NumberFormatException | L is source syntax, not part of numbers |
valueOf("042") | 42 | radix 10; leading zeros are just zeros |
NumberFormatException.2Whitespace and Signs
The first two calls look almost the same, but they behave differently.
parseInt treats a space like any other non-digit character, so " 42" fails. The fix is to call strip() (or trim()) on the input first, and it is worth doing at the boundary where text enters the system, because spaces often arrive from CSV cells and form fields. Notice the last line: Double.parseDouble does trim leading and trailing whitespace. Two sibling methods with different rules is exactly why this question gets asked.
A leading plus sign has been accepted since Java 7. Only one sign is allowed, and it must come first.
3Values Outside the int Range
The third call is just one too large.
parseInt checks the range while it reads the digits, so instead of wrapping around it throws. That is good behaviour: it fails loudly instead of returning a wrong number. When values may be bigger, we parse with Long.parseLong, and if we need an int later, Math.toIntExact converts and throws ArithmeticException if the value does not fit, rather than silently cutting off the high bits as a cast would.
4Other Bases and the Binary Output of -1
Now the radix argument and the reverse direction, turning an int into text in another base.
parseInt with radix 16 reads f as 15, so ff is 15 x 16 + 15 = 255. Notice that it does not accept the 0x prefix; that belongs to source literals and to Integer.decode.
toBinaryString(-1) shows how Java stores negative numbers. Ints use two's complement: to get -1, take 1, flip every bit and add one, which gives all 32 bits set. toBinaryString, toHexString and toOctalString print the bits as if the int were unsigned, so -1 becomes 32 ones (and ffffffff in hex). Integer.toString(-1, 2) is the signed version and prints -1.
5Source Syntax Is Not Parsing Syntax
The last two calls show that the rules for literals in source code do not apply to parsing.
In source code, 42L tells the compiler the literal is a long, and 042 means octal. Neither rule exists in parseLong or parseInt: L is just an invalid character, and leading zeros in radix 10 are ordinary zeros, so "042" is 42. Only Integer.decode follows the source-like rules, reading 0x, 0X or # as hex and a leading 0 as octal. That difference can make two parts of a system read the same configuration value differently.
6Follow-up questions
When would you use Integer.parseUnsignedInt?
When text holds an unsigned 32-bit value, such as a checksum, an IPv4 address as a number, or an id from a system written in C. parseUnsignedInt("4294967295") succeeds and returns the int -1, because the same 32 bits mean 4,294,967,295 when read as unsigned. We then keep it in an int and use the unsigned helpers: Integer.toUnsignedString(-1) gives 4294967295 back, Integer.toUnsignedLong widens it correctly, and Integer.compareUnsigned and divideUnsigned do unsigned arithmetic.
How does Integer.decode differ from parseInt?
decode reads the base from the text itself, like a source literal: 0x, 0X or # means hex, a leading 0 means octal, anything else decimal, with an optional sign in front. parseInt always uses the radix we pass, 10 by default, and rejects prefixes. So decode("010") is 8 while parseInt("010") is 10. decode is handy for reading constants written by people, such as colours like #ff8800, but for configuration and user input parseInt with an explicit radix is safer.
Is it better to catch NumberFormatException or validate first?
For untrusted input both are acceptable, but validating with a simple pattern first, such as optional sign and up to a fixed number of digits, gives clearer error messages to the caller. Catching the exception and converting it into a validation error at the boundary is also fine; what we must not do is let it escape as a 500 error.
7Conclusion
In this answer, we saw that parseInt is strict: it does not trim spaces, accepts one leading sign, and throws instead of overflowing, so " 42" and "2147483648" fail while "+42" is 42. The radix argument picks the base, so "ff" in base 16 is 255, and toBinaryString shows negative numbers as their unsigned two's complement bits, 32 ones for -1. Finally, source literal rules do not apply to parsing: "42L" fails and "042" is 42, and only Integer.decode reads leading zeros as octal.
Open with
parseInt is strict, so most of these come down to its contract. Space 42 throws NumberFormatException, because parseInt does not trim whitespace, so I would strip the input at the boundary first.
Then, if they want more
Interestingly, Double.parseDouble does trim, which is why people get surprised.
Plus 42 returns 42, since one leading sign is allowed. 2147483648 throws because it is one more than Integer.MAX_VALUE, and parseInt checks the range rather than wrapping; for bigger values I would use Long.parseLong and Math.toIntExact if I need an int later. parseInt of ff with radix 16 is 255, since the radix picks the base from 2 to 36 and f is 15. toBinaryString of minus 1 gives 32 ones, because negative ints are stored in two's complement and that method prints the bits as unsigned.
Long.parseLong of 42L throws, because the L suffix is only source syntax for the compiler, not part of the number format. And Integer.valueOf of 042 is 42, because parsing uses radix 10 and leading zeros are just zeros. Only Integer.decode follows source rules and would read 042 as octal 34 and 0x1F as 31.
For unsigned values, like a 32-bit checksum, I would use parseUnsignedInt and the toUnsignedString helpers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do these return or throw, and why?": https://scrivio-interview-library.netlify.app/java/types-and-values/a17047ac29/