Java Findbugs 问题与“装箱/拆箱以解析基元”与 Integer.valueOf(String)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32516893/
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-08-11 12:40:36  来源:igfitidea点击:

Findbugs issue with "Boxing/unboxing to parse a primitive" with Integer.valueOf(String)

javafindbugsboxingunboxing

提问by mac

I have this piece of code:

我有这段代码:

public void someMethod(String id) {
   someOtherMethod(Integer.valueOf(id));
}

public void someOtherMethod(int id) {
   // do something with id
}

And on that second line, Findbugs is throwing this exception:

在第二行,Findbugs 抛出这个异常:

Boxing/unboxing to parse a primitive

装箱/拆箱以解析原语

Why is Findbugs complaining about this when I'm simply calling Integer.valueOf() / how can I fix this?

为什么 Findbugs 在我只是调用 Integer.valueOf() 时会抱怨这个/我该如何解决这个问题?

回答by mac

It took me a while to figure that one out (partially because Jenkins just said "Boxing/unboxing to parse a primitive"), but apparently the problem / solution is in what Integer.valueOf() does internally, namely:

我花了一段时间才弄明白(部分是因为詹金斯只是说“装箱/拆箱来解析原语”),但显然问题/解决方案是 Integer.valueOf() 在内部所做的,即:

Integer.valueOf(parseInt(s, 10));

So the solution is to simply to call parseInt() directly instead:

所以解决方案是直接调用 parseInt() 来代替:

someOtherMethod(Integer.parseInt(id));

The detailed description of the issue (DM_BOXED_PRIMITIVE_FOR_PARSING) can be found on the findbugs page.

问题的详细描述 (DM_BOXED_PRIMITIVE_FOR_PARSING) 可以在findbugs 页面上找到。

回答by T.J. Crowder

The issue is that Integer.valueOfreturns an Integer, not an int, but your someOtherMethodexpects an int. Findbugs is basically warning you that you're doing it a long-winded way that involves potentially creating an object (the Integer) that you don't need which you're then immediately going to unbox by passing it to someOtherMethod(int), e.g.:

问题是Integer.valueOf返回的是Integer,而不是int,但您someOtherMethod期望的是int。Findbugs 基本上是在警告您,您正在以一种冗长的方式进行操作,其中涉及可能创建一个Integer您不需要的对象(),然后您将立即通过将其传递给 来取消装箱someOtherMethod(int),例如:

String => int => Integer => int
          ^^^^^^^^^^^^^^
                \--- This is inside Integer.valueOf

Instead, you can and probably should avoid that unnecessary round-trip through Integerand simply do:

相反,您可以而且可能应该避免不必要的往返Integer,只需执行以下操作:

String => int
^^^^^^^^^^^^^
      \--- Integer.parseInt

There's just no need for the temporary Integerand the potential memory allocation and such surrounding it.

只是不需要临时Integer和潜在的内存分配等等。

If someOtherMethodwere expecting an Integer, you wouldn't get the warning, because the Integerisn't purely temporary.

如果someOtherMethod期待Integer,您将不会收到警告,因为Integer不是纯粹暂时的。

This is just one of a class of unnecessary-boxing-conversions that Findbugs and tools like it helpfully point out.

这只是 Findbugs 和类似工具帮助指出的一类不必要的装箱转换之一。