The following expressions are valid in Java as obvious
int a = -0;
int b = +0;
and so are the following.
Integer c = new Integer(-0);
int d = Integer.parseInt("-0");
BigDecimal e = new BigDecimal("-0");
The following statements are however invalid.
Integer f = new Integer("+0"); //Leading + sign.
int g=Integer.parseInt("+0"); //Leading + sign.
Both of them throw the NumberFormatException.
The following statement with BigDecimal however compiles and runs without causing an exception to be thrown.
BigDecimal bigDecimal = new BigDecimal("+0"); //Leading + sign.
Why is a leading + sign valid with BigDecimal here which however doesn’t appear to be the case with the other datatypes available in Java?
According to the documentation, negative signs needs the minus sign. But if its a positive Integer, no need for a plus sign.
public static int parseInt(String s)Then for the constructor:
public Integer(String s)