java 为什么整数不解决空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15115772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:31:31  来源:igfitidea点击:

Why Integer doesn't solve null String

java

提问by hudi

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

  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 ?

  1. Integer.parseInt(null);参数:字符串返回:int,所以我期望参数为空时抛出 NPE

  2. Integer.valueOf(null);参数:字符串返回:整数,所以我期望当参数为空时这应该返回空。但是这个前提是错误的,我问为什么没有这样的方法可以处理这种情况?

回答by PermGenError

nullis 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()要求被解析的字符串是整数的有效表示。

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     }

回答by Gus

The folks at Sun who implemented Integer (a longtime 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 fastdesign)

Sun 实现 Integer 的人(long前一段时间 :) )在编写该方法时可能没有考虑数据库。除了在处理数据库数据时,或者在极少数情况下您试图用 null 显式表示“未知”之外,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 :)

基本上,您观察到的是目前的情况,有人将不得不付钱让您围绕它进行编码:)

回答by Jess

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);
}

回答by Jess

@hudi, what about this?

@hudi,这个怎么样?

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 是很痛苦的;如果有标准的东西就好了。