I am trying to parse +1
value to an int
value. In my code +1
is a String
and i am trying to convert it to Integer
using Integer.parseInt
,
String num = "+1";
int convertedNum = Integer.parseInt(num);
but I am getting error, why Java is not treating +1
as an integer value ?
because you must be using older version of JDK (JDK < 7). Before Java 7,
+1
wasn't considered as a valid integer.
Prior to
JDK 1.7
, what was the result of the following code segment?
double x = Double.parseDouble("+1.0");
int n = Integer.parseInt("+1");
Pat yourself on the back if you knew the answer:
+1.0
has always been a valid floating-point number, but until Java 7,+1
was not a valid integer. This has now been fixed for all the various methods that constructint
,long
,short
,byte
, andBigInteger
values from strings. There are more of them than you may think. In addition to parse (Int|Long|Short|Byte), there are decode methods that work with hexadecimal and octal inputs, andvalueOf
methods that yield wrapper objects. TheBigInteger(String)
constructor is also updated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With