Ternary Operators Java

2020-01-23 03:39发布

Is there a way to implement this in a ternary operation. I'm very new to that ternary stuff, maybe you could guide me.

if(selection.toLowerCase().equals("produkt"))
     cmdCse.setVisible(true);
else
     cmdCse.setVisible(false);

This one doesn't seem to work.

selection.toLowerCase().equals("produkt")?cmdCse.setVisible(true):cmdCse.setVisible(false);

6条回答
Fickle 薄情
2楼-- · 2020-01-23 04:01

Here are my tips, if you need to set things to booleans, then simple use setBoolean(condition), else, if you need to set a variable to a non boolean value, then use var=condition?result1:result2(or the variable itself if you don't want to change if condition is false), otherwise, use if else.

查看更多
走好不送
3楼-- · 2020-01-23 04:10

Perhaps

cmdCse.setVisible(selection.toLowerCase().equals("produkt"));
查看更多
We Are One
4楼-- · 2020-01-23 04:13

The ternary operator isn't exactly like an if statement. A ternary operator has to "return" something from both sides, so putting void method calls like setVisible() there won't work.

Instead, you could do something like this without ternary operators at all:

cmdCse.setVisible(selection.toLowerCase().equals("product"));

But just to demonstrate the point, the ternary equivalent would look something like this:

cmdCse.setVisible(selection.toLowerCase().equals("product") ? true : false);

Notice how now the ternary operator "returns" true or false on both sides instead of simply calling a void method.

查看更多
神经病院院长
5楼-- · 2020-01-23 04:16

In this case, you don't even need a ternary operator:

 cmdCse.setVisible(selection.toLowerCase().equals("produkt"));

Or, cleaner:

 cmdCse.setVisible(selection.equalsIgnoreCase("produkt"));

Your version:

selection.toLowerCase().equals("produkt")? cmdCse.setVisible(true): cmdCse.setVisible(false);

is semantically incorrect: ternary operator should represent alternative assignments, it's not a full replacement for if statements. This is ok:

double wow = x > y? Math.sqrt(y): x;

because you are assigning either x or Math.sqrt(y) to wow, depending on a condition.

My 2cents: use ternary operator only when it makes your program clearer, otherwise you will end up having some undecipherable one-liners.

查看更多
在下西门庆
6楼-- · 2020-01-23 04:21

I think this will work for you

cmdCse.setVisible(selection.toLowerCase().equals("produkt"));
查看更多
相关推荐>>
7楼-- · 2020-01-23 04:22

Directly from the docs

Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

In your case cmdCse.setVisible(true / false); doesn't return anything, and the operation also has side effects (it changes state of cmdCse), so the conditional operator cannot be used here (when you do use the operator, both of the ? and : branches must have the same return type).

As an aside, please note that .. ? .. : .. should be referred to as the conditional operator

查看更多
登录 后发表回答