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

发布于 2024-09-27 03:28:18 字数 208 浏览 1 评论 0原文

如果你有一个布尔变量:

boolean myBool = true;

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

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

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

If you have a boolean variable:

boolean myBool = true;

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

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

Is there a more concise way to do this?

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

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

发布评论

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

评论(5

依 靠 2024-10-04 03:28:18

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

myBool = !myBool;

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 true to false (and vice versa):

myBool = !myBool;
吲‖鸣 2024-10-04 03:28:18

一种更酷的方法(如果您想设置变量,那么对于长度超过 4 个字符的变量名,这比 myBool = !myBool 更简洁):

myBool ^= true;

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

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

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).

_失温 2024-10-04 03:28:18

对于 boolean 来说,这非常简单,对于 Boolean 来说,则有点具有挑战性。

  • boolean 只有 2 种可能的状态:
  • 另一方面,Boolean 有 3 个:Boolean.TRUE
    Boolean.FALSEnull

假设您只是处理一个 boolean (这是一个原始类型),那么最简单的事情是:

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

但是,如果您想反转一个 Boolean (这是一个对象),您必须注意 null 值,否则最终可能会出现 NullPointerException

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

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

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

但是,如果您确实也想处理 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);

另一方面,如果您希望 null 在反转后保持 null,那么您可能需要考虑使用 apache commonsBooleanUtils(参见 javadoc

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

有些人更喜欢将其全部写出来,以避免对 apache 的依赖。

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

For a boolean it's pretty easy, a Boolean is a little bit more challenging.

  • A boolean only has 2 possible states:
    trueand false.
  • A Boolean on the other hand, has 3: Boolean.TRUE,
    Boolean.FALSE or null.

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

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 null value, or you may end up with a 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 null values as well. Then there are several interpretations.

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 null to stay null after inversion, then you may want to consider using the apache commons class BooleanUtils(see 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.

Boolean someValue = null; // or Boolean.TRUE or Boolean.FALSE;
Boolean negative = (someValue == null)? null : Boolean.valueOf(!someValue.booleanValue());
淡淡的优雅 2024-10-04 03:28:18

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

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.

妄断弥空 2024-10-04 03:28:18
myBool = myBool ? false : true;
myBool = myBool ? false : true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文