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:没有回报的三元.(用于方法调用)
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
