简体   繁体   中英

When would you use the conditional operator in java (? :) instead of the if else statement?

I have a piece of code that uses a conditional operator to compare the value of the variable x to 5:

(x >=5 ? x : -x)

What benefit is there to using a conditional operator over a regular if else statement?

Note that is ?: is an expression - it returns a value which must be consumed

z  = (x >=5 ? x : -x)

The if construct in Java is not an expression (there are languages where it is) and does not return a value so in this sense they are not equivalent.

An example where they are equivalent is when the if options perform an assignment:

if("Cheese".equals(x)) {
    type = "Dairy";
} else {
    type = "Maybe not dairy";
}

this is the same as

type =  "Cheese".equals(x) ? "Dairy" : "Maybe not dairy"; 

You can nest ?: to arbitrary depth but really shouldn't - it becomes quite difficult to read:

    List<String> cheeses  = Arrays.asList("Gouda", "Edam");
    String x= "Gouda";

    String type =  cheeses.contains(x) ? "Gouda".equals(x) ? "Yummy Gouda" : "Cheese - but not Gouda" : "Maybe not dairy";

Ternary operator is not the equivalent of if-else in EVERY possible case. This is because both of possible results of using ternary operator are return values (eg simple printing to the console is not a return value so it can't be one of possible results in ternary operator).

Look, you can do it with if-else , but it's not possible with ternary operator:

if (x > 5) {
    System.out.println("Statement is true");
else {
    System.out.println("Statement is false");
}

You can't do it with ternary operator:

x > 5 ? System.out.println("Statement is true") : System.out.println("Statement is false");

When both of results of using ternary operator are considered as return values, they are then an equivalent of if-else .

Yes, it can be used, but off course in that case using -x doesn't make sense so it depends on what do you want to return when String is an input. You can do this:

("Cheese".equals(x) ? <some object of expected type X> : <some other object of expected type X>)

where X can be String on any other type. And remember to do "string".equals(x) instead of x.equals("string") . This prevents NullPointerException .

For primitive values you can use == , but for objects you need to use equals method. There are some edge cases when you can use == on String , but they are not relevant to your case I guess and you can read about it outside of this topic.

PS: Some answers talk about if else statement, but that's not what question is about.

Can this be used like an if else on all accounts? For example, can you compare strings with it?

I am pretty sure that by saying like an if else he means conditional operator which has if else like behaviour, not if else instruction itself.

Yes it can be used in Java; however note that x =="Cheese" isn't the proper way to compare strings in Java, you want something like "Cheese".equals(x) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM