Ternary Operators Java

匆匆过客 提交于 2019-11-26 02:02:57

问题


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);

回答1:


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.




回答2:


Perhaps

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



回答3:


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.




回答4:


I think this will work for you

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



回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/21219695/ternary-operators-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!