samedi 24 février 2018

Can you use the conditional operator in java (? :) in place of the if else statement?

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

(x >=5 ? x : -x)

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

(x =="Cheese" ? x : -x)

Aucun commentaire:

Enregistrer un commentaire