Why does this code throw NumberFormatException?
int a = Integer.parseInt("1111111111111111111111111111111111111111");
How to get the value of int for that String?
The value that you're attempting to parse is much bigger than the biggest allowable int value (Integer.MAX_VALUE, or 2147483647), so a NumberFormatException is thrown. It is bigger than the biggest allowable long also (Long.MAX_VALUE, or 9223372036854775807L), so you'll need a BigInteger to store that value.
BigInteger veryBig = new BigInteger("1111111111111111111111111111111111111111");
From BigInteger Javadocs:
Immutable arbitrary-precision integers.
This is because the number string is pretty large for an int . Probably this requires a BigInteger .
String(viewed from the perspective as anint) exceedsInteger.MAX_VALUE(which is 2^31).Stringis a not a validint.inthas a maximum value of 2,147,483,647. Are you trying to parse a binary representation instead? If you are, you've got 40 bits there, so it still won't fit (int only stores 32).