What#39;s the most concise way to get the inverse of a Java boolean value?(获取 Java 布尔值的倒数的最简洁方法是什么?)
问题描述
如果你有一个布尔变量:
If you have a boolean variable:
boolean myBool = true;
我可以通过 if/else 子句得到相反的结果:
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?
推荐答案
只需使用逻辑非运算符 !
进行赋值,就像您在条件语句(if
, for
, while
...).您已经在使用布尔值,因此它会将 true
翻转为 false
(反之亦然):
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;
这篇关于获取 Java 布尔值的倒数的最简洁方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 Java 布尔值的倒数的最简洁方法是什么?


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01