简体   繁体   English

像 !Boolean.FALSE.equals() 这样的表达式有什么理由吗?

[英]Is there any reason for expression like !Boolean.FALSE.equals()?

When browsing the Spring's source code, I encountered code like if (!Boolean.FALSE.equals(pv.conversionNecessary)) {...}在浏览 Spring 的源码时,遇到了if (!Boolean.FALSE.equals(pv.conversionNecessary)) {...}

After some research, I also found codes like !Boolean.FALSE.equals and !Boolean.TRUE.equals exist in other open-source projects too.经过一番研究,我还发现其他开源项目中也存在诸如!Boolean.FALSE.equals!Boolean.TRUE.equals类的代码。

I wonder what's the possible reasons for not using the apparently concise expression?我想知道不使用明显简洁的表达的可能原因是什么?

There are various reasons why this kind of thing might be necessary.这种事情可能有必要的原因有多种。

In this example, pv is an instance of PropertyValue which declares the conversionNecessary as follows:在这个例子中, pv是一个PropertyValue的实例,它声明了conversionNecessary如下:

/** Package-visible field that indicates whether conversion is necessary. */
@Nullable
volatile Boolean conversionNecessary;

( source code ) 源代码

From this, we can infer that it is not unexpected for conversionNecessary to be null .由此我们可以推断, conversionNecessarynull并不意外。

So ... the verbose form if (!Boolean.FALSE.equals(pv.conversionNecessary)) {...} is dealing with the null case.所以......冗长的形式if (!Boolean.FALSE.equals(pv.conversionNecessary)) {...}正在处理null情况。 Boolean.FALSE.equals(null) is false . Boolean.FALSE.equals(null)false

By contrast, the more concise form if (!pv.conversionNecessary) {...} would throw an NPE.相比之下,更简洁的形式if (!pv.conversionNecessary) {...}会抛出 NPE。

It is also worth noting that since conversionNecessary is volatile here, an alternative version that did an explicit null check would need to use a temporary variable to avoid race conditions;还值得注意的是,由于conversionNecessary在这里是volatile ,执行显式null检查的替代版本将需要使用临时变量来避免竞争条件; eg例如

Boolean necessary = pv.conversionNecessary;
if (necessary == null || necessary) { ... }

Another scenario where you might do this kind of thing is if the declared type of the variable is Object rather than Boolean .您可能会做这种事情的另一种情况是,如果变量的声明类型是Object而不是Boolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 中的“Boolean.TRUE.equals(x)”有什么原因吗? - Is there any reason for "Boolean.TRUE.equals(x)" in Java? 为什么这个布尔表达式是假的? - Why is this boolean expression false? 是否有正当理由在java中将false布尔值编码为“Boolean.FALSE”? - Is there a valid reason to code a false boolean as “Boolean.FALSE” in java? 是否存在`==`为真但`equals`为假的情况? - Are there any scenarios where `==` is true but `equals` is false? 如何在不使用.equals和错误的布尔值的情况下比较数组 - How to compare arrays without using .equals and with a false boolean 根据布尔值是true还是false来删除/添加JComponent - Removing/Adding JComponents depending on if boolean equals to true or false 当 boolean 表达式为假时,for 循环仍然有效 - For loop still working when boolean expression is false 我的 Boolean 由于某种原因被指定为 false 时设置为 true - my Boolean is set to true when its specified as false for some reason 比较Java Strings.equals()方法会产生不正确的布尔表达式 - Comparing Java Strings.equals() method yields incorrect Boolean Expression 在生成 .equals() 时,有什么理由更喜欢 getClass() 而不是 instanceof? - Any reason to prefer getClass() over instanceof when generating .equals()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM