不正确,表示通过PMD发出警告

问题描述 投票:2回答:1

对于以下代码

boolean changed = null == oldValue? oldValue != newValue : !oldValue.equals(newValue);

我收到PMD警告:“使用equals()比较对象引用”。但是,如果我遵循这个建议,我将获得NPE。是PMD规则的错误还是我的编程风格很糟糕?

java nullpointerexception nullreferenceexception pmd
1个回答
5
投票

只需稍微更改一下:

boolean changed = null == oldValue ? newValue != null : !oldValue.equals(newValue);
© www.soinside.com 2019 - 2024. All rights reserved.