Java自动拆箱-有编译器警告吗?

发布于 2024-08-18 00:48:04 字数 404 浏览 2 评论 0原文

我非常喜欢 Java 中的自动装箱,因为它可以节省大量丑陋的样板代码。然而,我发现在某些情况下自动拆箱会令人困惑,其中 Number 对象可能为空。有没有什么方法可以检测代码库中发生自动拆箱并带有 javac 警告的位置?任何其他仅检测拆箱情况的解决方案(例如 FindBugs 或 Eclipse 特定的编译器警告)将不胜感激,因为我找不到任何解决方案。

为了澄清,我不希望在装箱时生成任何警告 - 仅在拆箱时生成。

下面是一些可能导致令人困惑的 NullPointerExceptions 的代码的简单示例:

class Test {
    private Integer value;

    public int getValue() {
        return value;
    }
}

I am a big fan of auto-boxing in Java as it saves a lot of ugly boiler plate code. However I have found auto-unboxing to be confusing in some circumstances where the Number object may be null. Is there any way to detect where auto-unboxing is occurring in a codebase with a javac warning? Any other solution to detect occurrences of unboxing only (such as FindBugs or Eclipse-specific compiler warning) would be appreciated as I cannot find any.

To clarify I do not want any warnings to be generated on boxing - only unboxing.

Here is a simple example of some code that can cause confusing NullPointerExceptions:

class Test {
    private Integer value;

    public int getValue() {
        return value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赴月观长安 2024-08-25 00:48:04

是的。在 Eclipse 中:

首选项 -> Java -> 编译器 -> 错误/警告 -> 潜在编程问题 -> 装箱和拆箱转换

Yup. In Eclipse:

Preferences->Java->Compiler->Errors/Warnings->Potential Programming Problems->Boxing and unboxing conversions

苍白女子 2024-08-25 00:48:04

不幸的是没有。这是自动拆箱号码的问题之一。您可以

  • 将其初始化为默认值,例如 private Integer value = 0
  • 检查是否为 null 返回值 != null ? value : 0

我个人更喜欢第一种方法。一般来说,我认为不会有太多应该有空号码的情况。

另外,为什么使用大整数来存储值。如果你只返回一点 int,为什么不这样存储呢?

Unfortunately there is not. This is one of the issues with auto unboxing numbers. You can

  • Initialize it to a default value, such as private Integer value = 0
  • Check for a null return value != null ? value : 0

Personally I prefer the first method. In general I would think you wouldn't have too many cases where you should have a null number.

Also, why are you using a big Integer to store the value. If you're only returning a little int, why not store it that way?

多彩岁月 2024-08-25 00:48:04

Eclipse 将允许您进行语法颜色装箱和拆箱操作(但不是其中之一)。我将它们设置为亮红色:如果发生任何一种情况,则意味着我在匹配参数和参数方面很草率。

Eclipse will let you syntax-color boxing and unboxing operations (but not one or the other). I have them set to bright red: if either happens, it means that I've been sloppy in matching parameters and arguments.

蓝咒 2024-08-25 00:48:04

我怀疑这会是一个警告案例。这是自动装箱的怪癖之一。

Joshua Bloch 在他的《Effective Java》一书中谈到了这一点。基本上,编译器正在尝试为您做更多超出预期的事情。换句话说,此类问题通常与使用相关,因此很难“发现错误”,因为从语义上讲,错误根本不存在。

I doubt that this will be a warning case. This is one of the quirks of auto-boxing.

Joshua Bloch addresses this in his book "Effective Java". Basically, the compiler is trying to do more for you that what is expected. In other words, this type of issue is more commonly related to usage and as a result, it is difficult to "spot the error" as the error is, semantically speaking, simply not there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文