为什么Integer不解决null String

时间:2022-09-06 08:12:17

Integer class contains some method to convert String into Integer:

Integer类包含一些将String转换为Integer的方法:

  1. Integer.parseInt(null); Parameter: String Return: int, so I expected when parameter is null this throw NPE

    的Integer.parseInt(NULL);参数:String返回:int,所以我期望当参数为null时抛出NPE

  2. Integer.valueOf(null); Parameter: String Return: Integer,so I expected when parameter is null this should return null. But this precondition was wrong and I ask why there is no such method which can handle this situation ?

    Integer.valueOf(NULL);参数:String返回:整数,所以我预计当参数为null时,它应该返回null。但这个先决条件是错误的,我问为什么没有这样的方法可以处理这种情况呢?

4 个解决方案

#1


8  

The folks at Sun who implemented Integer (a long time ago :) ) probably were not thinking of databases when they wrote that method. Except when dealing with database data, or rare cases where you are trying to explicitly represent "unknown" with null, null is usually a sign of something gone terribly wrong. In general, it's a good idea to raise an exception as soon as there is a problem. (a Fail fast design)

Sun实施Integer(很久以前:))的人可能在编写该方法时没有想到数据库。除了在处理数据库数据时,或者在尝试使用null显式表示“unknown”的极少数情况下,null通常是出现严重错误的迹象。一般来说,一旦出现问题就提出异常是个好主意。 (失败快速设计)

If you have ever spent time hunting down a segmentation fault in C that is due to a string value longer than the memory you supplied for it (which then overwrote some program code or other data) you will have a very good appreciation of how bad it is to not fail when something has gone wrong.

如果您曾经花时间寻找C中的分段错误,这是由于字符串值超过您为其提供的内存(然后覆盖了一些程序代码或其他数据),您将非常了解它有多糟糕当出现问题时,不要失败。

Since the time of Java 1.0, interaction with databases in java has become extremely common so you might be right to suggest that there should be a method to handle this. Integer is a final class so if you build your own Integer like class you will loose autoboxing, so this probably does require a change to the language by oracle.

从Java 1.0开始,与java中的数据库的交互变得非常普遍,所以你可能会建议应该有一个方法来处理这个问题。 Integer是一个最终类,所以如果你构建自己的类Integer,你将失去自动装箱,所以这可能需要oracle改变语言。

Basically, what you observed is the way it is for now, and someone will have to pay you to code around it :)

基本上,你观察到的是现在的方式,有人将不得不付钱给你代码:)

#2


11  

null is not a valid representation of integer number. Integer.parseInt() requires that the string be parsed is a vaild representation of integer number.

null不是整数的有效表示。 Integer.parseInt()要求解析的字符串是整数的vaild表示。

Integer.parseInt()

的Integer.parseInt()

  public static int parseInt(String s, int radix)
440                 throws NumberFormatException
441     {
442         if (s == null) {
443             throw new NumberFormatException("null");
444         }

Integer.valueOf(str)] // which inviokes Integer.parseInt(Str) to return an Integer instance.

Integer.valueOf(str)] //调用Integer.parseInt(Str)返回一个Integer实例。

  public static Integer valueOf(String s) throws NumberFormatException
569     {
570         return new Integer(parseInt(s, 10));
571     }

#3


2  

I could be wrong, @hudi, but is this what you are looking for?

我可能是错的,@ hudi,但这是你在找什么?

public Integer valueOf2( String inputString ) {
    return (inputString == null) ? null : Integer.parseInt(inputString);
}

#4


0  

@hudi, what about this?

@hudi,这个怎么样?

IntegerConverter

IntegerConverter

I've never used it, but it looks like you can specify a default value if there is a conversion error. I know it is a pain to include another jar in your project just for this; It would be nice if there was something standard.

我从来没有使用它,但看起来你可以指定一个默认值,如果有转换错误。我知道在你的项目中包含另一个jar只是为了这个很痛苦;如果有标准的东西会很好。

#1


8  

The folks at Sun who implemented Integer (a long time ago :) ) probably were not thinking of databases when they wrote that method. Except when dealing with database data, or rare cases where you are trying to explicitly represent "unknown" with null, null is usually a sign of something gone terribly wrong. In general, it's a good idea to raise an exception as soon as there is a problem. (a Fail fast design)

Sun实施Integer(很久以前:))的人可能在编写该方法时没有想到数据库。除了在处理数据库数据时,或者在尝试使用null显式表示“unknown”的极少数情况下,null通常是出现严重错误的迹象。一般来说,一旦出现问题就提出异常是个好主意。 (失败快速设计)

If you have ever spent time hunting down a segmentation fault in C that is due to a string value longer than the memory you supplied for it (which then overwrote some program code or other data) you will have a very good appreciation of how bad it is to not fail when something has gone wrong.

如果您曾经花时间寻找C中的分段错误,这是由于字符串值超过您为其提供的内存(然后覆盖了一些程序代码或其他数据),您将非常了解它有多糟糕当出现问题时,不要失败。

Since the time of Java 1.0, interaction with databases in java has become extremely common so you might be right to suggest that there should be a method to handle this. Integer is a final class so if you build your own Integer like class you will loose autoboxing, so this probably does require a change to the language by oracle.

从Java 1.0开始,与java中的数据库的交互变得非常普遍,所以你可能会建议应该有一个方法来处理这个问题。 Integer是一个最终类,所以如果你构建自己的类Integer,你将失去自动装箱,所以这可能需要oracle改变语言。

Basically, what you observed is the way it is for now, and someone will have to pay you to code around it :)

基本上,你观察到的是现在的方式,有人将不得不付钱给你代码:)

#2


11  

null is not a valid representation of integer number. Integer.parseInt() requires that the string be parsed is a vaild representation of integer number.

null不是整数的有效表示。 Integer.parseInt()要求解析的字符串是整数的vaild表示。

Integer.parseInt()

的Integer.parseInt()

  public static int parseInt(String s, int radix)
440                 throws NumberFormatException
441     {
442         if (s == null) {
443             throw new NumberFormatException("null");
444         }

Integer.valueOf(str)] // which inviokes Integer.parseInt(Str) to return an Integer instance.

Integer.valueOf(str)] //调用Integer.parseInt(Str)返回一个Integer实例。

  public static Integer valueOf(String s) throws NumberFormatException
569     {
570         return new Integer(parseInt(s, 10));
571     }

#3


2  

I could be wrong, @hudi, but is this what you are looking for?

我可能是错的,@ hudi,但这是你在找什么?

public Integer valueOf2( String inputString ) {
    return (inputString == null) ? null : Integer.parseInt(inputString);
}

#4


0  

@hudi, what about this?

@hudi,这个怎么样?

IntegerConverter

IntegerConverter

I've never used it, but it looks like you can specify a default value if there is a conversion error. I know it is a pain to include another jar in your project just for this; It would be nice if there was something standard.

我从来没有使用它,但看起来你可以指定一个默认值,如果有转换错误。我知道在你的项目中包含另一个jar只是为了这个很痛苦;如果有标准的东西会很好。