Java: Ternary with no return. (For method calling)(Java:没有回报的三元.(用于方法调用))
问题描述
我想知道是否可以进行三元运算但不返回任何内容.
I was wondering if it was possible to do a ternary operation but without returning anything.
如果在 Java 中不可能,那么在其他语言中是否可能,如果可以,哪些适用?
If it's not possible in Java is it possible in other languages, if so which ones apply?
name.isChecked() ? name.setChecked(true):name.setChecked(false);
推荐答案
不,你不能.但是,与 if-else
语句相比,这有什么意义呢?您真的要保存 7 个字符吗?
No, you can't. But what's the point of this over an if-else
statement? Are you really trying to save 7 characters?
if (name.isChecked()) {
name.setChecked(true);
} else {
name.setChecked(false);
}
或者如果你喜欢糟糕的风格:
or if you prefer bad style:
if (name.isChecked()) name.setChecked(true); else name.setChecked(false);
别介意你可以做(在这种情况下):
Never mind the fact that you can just do (in this case):
name.setChecked(name.isChecked());
三元或条件"运算符的重点是将条件引入表达式.换句话说,这是:
The point of the ternary or "conditional" operator is to introduce conditionals into an expression. In other words, this:
int max = a > b ? a : b;
是这个的简写:
int max;
if ( a > b ) {
max = a;
} else {
max = b;
}
如果没有产生值,则条件运算符不是快捷方式.
If there is no value being produced, the conditional operator is not a shortcut.
这篇关于Java:没有回报的三元.(用于方法调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java:没有回报的三元.(用于方法调用)
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01