包装器构造函数获取 NumberFormatException:

The Wrapper Constructors get NumberFormatException:

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

在上面的代码中,我没有遇到任何异常。但是下面的代码我得到了 NumberFormatException:

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

我知道所有的包装器类 除了 Character 提供两个构造函数

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

所以为什么我得到这个例外

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

为什么没有

Float f2 = new Float("3.14f");

查看有关构造函数的文档

对于 Long,它遵循 Long.parseLong,后者不接受以 L 结尾的字符串。但是,Float 具有不同的解析行为,具体取决于它是随 3.14d 还是 3.14f 一起提供的,因此这些都是该特定构造函数的有效输入。