1
Float f1 = new Float("12.6f");

In this above code I didn't get any exception. But the below code I got NumberFormatException:

Long l1= new Long("200L"); 

I know all of the wrapper classes except Character provide two constructors

Integer i1 = new Integer(42); //Primitive
Integer i2 = new Integer("42"); // String
Float f1 = new Float(3.14f); //Primitive
Float f2 = new Float("3.14f"); // String

So why I get exception for this

Long l1= new Long("200L");  

why this didn't

Float f2 = new Float("3.14f");
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30

1 Answers1

2

Check the documentation on the constructors

For Long, it defers to Long.parseLong, which does not accept strings ending in L. However Float has different parsing behavior depending on whether it is provided with 3.14d or 3.14f, so these are both valid inputs for that specific constructor.

Kiskae
  • 24,655
  • 2
  • 77
  • 74