Java:三元不归.(对于方法调用) [英] Java: Ternary with no return. (For method calling)

查看:23
本文介绍了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:三元不归.(对于方法调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆