获取 Java 布尔值的倒数的最简洁方法是什么?

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

What's the most concise way to get the inverse of a Java boolean value?

javabooleaninverse

提问by faq

If you have a boolean variable:

如果你有一个布尔变量:

boolean myBool = true;

I could get the inverse of this with an if/else clause:

我可以用 if/else 子句得到相反的结果:

if (myBool == true)
 myBool = false;
else
 myBool = true;

Is there a more concise way to do this?

有没有更简洁的方法来做到这一点?

采纳答案by BoltClock

Just assign using the logical NOT operator !like you tend to do in your condition statements (if, for, while...). You're working with a boolean value already, so it'll flip trueto false(and vice versa):

只需使用逻辑 NOT 运算符进行赋值,!就像您在条件语句 ( if, for, while... ) 中经常做的那样。您已经在使用布尔值,因此它将翻转truefalse(反之亦然):

myBool = !myBool;

回答by fortran

An even cooler way (that is more concise than myBool = !myBoolfor variable names longer than 4 characters if you want to setthe variable):

一种更酷的方式(myBool = !myBool如果要设置变量,则比长度超过 4 个字符的变量名称更简洁):

myBool ^= true;

And by the way, don't use if (something == true), it's simpler if you just do if (something)(the same with comparing with false, use the negation operator).

顺便说一句,不要使用if (something == true),如果你只是这样做会更简单if (something)(与 false 比较相同,使用否定运算符)。

回答by Jinjavacoder

myBool = myBool ? false : true;

回答by Daniel Widdis

The most concise way is to not invert the boolean, and just use !myBool later on in the code when you want to check the opposite condition.

最简洁的方法是不要反转布尔值,当您想检查相反的条件时,稍后在代码中使用 !myBool 即可。

回答by bvdb

For a booleanit's pretty easy, a Booleanis a little bit more challenging.

对于 aboolean这很容易, aBoolean有点更具挑战性。

  • A booleanonly has 2 possible states: trueand false.
  • A Booleanon the other hand, has 3: Boolean.TRUE, Boolean.FALSEor null.
  • Aboolean只有两种可能的状态: truefalse
  • Boolean另一方面,A有 3: Boolean.TRUE, Boolean.FALSEnull

Assuming that you are just dealing with a boolean(which is a primitive type) then the easiest thing to do is:

假设您只是在处理 a boolean(这是一种原始类型),那么最简单的方法是:

boolean someValue = true; // or false
boolean negative = !someValue;

However, if you want to invert a Boolean(which is an object), you have to watch out for the nullvalue, or you may end up with a NullPointerException.

但是,如果您想反转 a Boolean(它是一个对象),则必须注意该null值,否则可能会以NullPointerException.

Boolean someValue = null;
Boolean negativeObj = !someValue.booleanValue(); --> throws NullPointerException.

Assuming that this value is never null, and that your company or organization has no code-rules against auto-(un)boxing. You can actually just write it in one line.

假设此值永远不会为空,并且您的公司或组织没有针对自动(取消)装箱的代码规则。您实际上可以将其写在一行中。

Boolean someValue = Boolean.TRUE; // or Boolean.FALSE
Boolean negativeObj = !someValue;

However if you do want to take care of the nullvalues as well. Then there are several interpretations.

但是,如果您确实想处理这些null值。然后有几种解释。

boolean negative = !Boolean.TRUE.equals(someValue); //--> this assumes that the inverse of NULL should be TRUE.

// if you want to convert it back to a Boolean object, then add the following.
Boolean negativeObj = Boolean.valueOf(negative);

On the other hand, if you want nullto stay nullafter inversion, then you may want to consider using the apache commonsclass BooleanUtils(see javadoc)

另一方面,如果您想在反转后null保留null,那么您可能需要考虑使用apache commons该类BooleanUtils请参阅 javadoc

Boolean someValue = null; // or Boolean.TRUE or Boolean.FALSE;
Boolean negativeObj = BooleanUtils.negate(someValue);

Some prefer to just write it all out, to avoid having the apache dependency.

有些人更喜欢把它全部写出来,以避免有 apache 依赖。

Boolean someValue = null; // or Boolean.TRUE or Boolean.FALSE;
boolean negative = (someValue == null)? null : !someValue.booleanValue();
Boolean negativeObj = Boolean.valueOf(negative);