0

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?

user18139
  • 188
  • 1
  • 3
  • 13
  • 2
    And what in your opinion would be `-"SomeString"`? – zlakad Feb 24 '18 at 12:46
  • 3
    If `x` is a `String` (You shouldn't compare Strings with `==` btw.), then what is `-x` supposed to be? – QBrute Feb 24 '18 at 12:48
  • @QBrute You're correct. My mistake. I updated the question now to use equalTo instead, and changed -x to y. If it is -x (or y, now), it would associate to a second case, where the first case would be x. x could be defined in the beginning of the code as something like String x = "It is cheese" and String y = "It's not cheese. I don't know what it is". – user18139 Feb 24 '18 at 14:12
  • Possible duplicate of [Ternary Operators Java](https://stackoverflow.com/questions/21219695/ternary-operators-java) – Bernhard Barker Feb 24 '18 at 14:28
  • [Why doesn't this method work? Java ternary operator](https://stackoverflow.com/q/16876698). [ternary operator not working](https://stackoverflow.com/q/17635940). [Java ternary operator (?:) doesn't work; second or third operand return boolean](https://stackoverflow.com/q/19010399). [Ternary operator to return value- Java/Android](https://stackoverflow.com/q/25072706). [How to use ternary operator with new?](https://stackoverflow.com/q/41871767) [What is the Java ?: operator called and what does it do?](https://stackoverflow.com/q/798545) – Bernhard Barker Feb 24 '18 at 14:29
  • What's stopping you from running your code to see if it works? – Bernhard Barker Feb 24 '18 at 14:31

4 Answers4

3

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";
slartidan
  • 20,403
  • 15
  • 83
  • 131
David Soroko
  • 8,521
  • 2
  • 39
  • 51
  • True - amended answer for the safer approach – David Soroko Feb 24 '18 at 13:17
  • Cool they're the same. What if you want to nest another if inside of the first if? so if it equals Cheese, then it asks what kind of cheese? That's the part I'm stuck on. If converting that nested if to a conditional, without using if. – user18139 Feb 24 '18 at 14:13
1

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 (e. g. 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.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • System.out.println((x > 5) ? "Statement is true" : "Statement is false"); –  Mar 04 '22 at 04:33
0

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.

ctomek
  • 1,696
  • 16
  • 35
-1

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

Simon Berthiaume
  • 643
  • 4
  • 11