【问题标题】:The Wrapper Constructors get NumberFormatException:Wrapper 构造函数得到 NumberFormatException:
【发布时间】:2018-07-20 14:27:32
【问题描述】:
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");

【问题讨论】:

    标签: java wrapper numberformatexception


    【解决方案1】:

    查看构造函数的文档

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2013-10-13
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多