Ternary operators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Ternary operators

Let's say a have a table with some double data, EX: 81.9-100%. GOOD 21-80%. BAD 13-21%. EVIL okay I want a way to beautifully write this in Java I was thinking about ternary operators but it seems you need an else and that's not my case, right? These are more like else if. The return should be the string Better solutions are welcome too

10th Jul 2021, 11:15 PM
Jace🎭
Jace🎭 - avatar
14 Answers
+ 8
you can chain ternary operator, even if is not advisable, as quickly becomes hardly readable: 81.9<=v && v<=100 ? "GOOD" : 21<=v && v<= 80 ? "BAD" : 13<=v && v<= 21 ? "EVIL" : ""
10th Jul 2021, 11:24 PM
visph
visph - avatar
+ 11
Jeison If you're just getting started, you may want to spend a bit of time in C#. Having worked with both Java and C# since their respective initial releases, I continue to remain far more satisfied with the language design decisions of C# over that of Java. If you have to work with the JVM, then fortunately, Kotlin is an excellent alternative and rivals very closely with that of C# in more ways than not. Funny enough... I vowed three different times since 2010 that I would personally not get involved with another Java enterprise project again only to be lured back in for one reason or another. Each time only reinforced my complete dissatisfaction working with the language - especially on large enterprise projects. Now that .NET is open source and multi-platform and NodeJS has a prominent foothold in enterprise, and Kotlin is now Google's preferred language for Android, I will never go back to another Java project again. But... that's just me. 😉
11th Jul 2021, 4:37 AM
David Carroll
David Carroll - avatar
+ 8
visph - While I, too, prefer the traditional syntax: //Ternary in Java / C# var status = (score > 70) ? "p" : "f"; I'm actually okay with Kotlin's approach: //Ternary in Kotlin val status = if (score > 70) "p" else "f" But... Python's approach kills me: #Ternary in Python status = "p" if (score > 70) else "f" Placing the value to assign when true prior to the conditional expression trips me up every time. 😂
11th Jul 2021, 5:21 AM
David Carroll
David Carroll - avatar
+ 7
I was pretty happy when switch expressions were added to C# as an alternative for more complex ternary expressions. v switch { _ when v >= 81.9 && v <= 100 => "GOOD", _ when v >= 21 && v <= 80 => "BAD", _ when v >= 13 && v <= 21 => "EVIL", _ => string.Empty }; https://code.sololearn.com/cp15izbviBZj/?ref=app While it's not Java related, I figured you'd find it interesting. 😉
11th Jul 2021, 2:43 AM
David Carroll
David Carroll - avatar
+ 7
Ipang Ah... yes... I should have clarified that Java has a switch expression. However, Java only supports evaluating against constants only, whereas C# has fantastic support for various types of operator patterns. In fact, in C# 9, which isn't yet supported in SoloLearn, the following switch expression would have been valid: var result = score switch { >= 81.9 and <= 100 => "GOOD", >= 21 and <= 80 => "BAD", >= 13 and <= 21 => "EVIL", _ => string.Empty }; This is only scratching the surface of switch expressions and patterns support in C#. These combined make for a powerful feature. Here are some links for additional information: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression
11th Jul 2021, 5:38 AM
David Carroll
David Carroll - avatar
+ 5
Ipang - Indeed... I didn't think you were "posting to compare Java against C# by feature." Rather, I thought you were suggesting the only limitation with the Java switch expression might be the use of floating points. I was only trying to clarify that it was actually Java's limited support for pattern matching that was the primary reason for it not being able to do something similar to the C# switch expression I posted earlier. Anyway, in revisiting my earlier response, I think I missed the mark on making that point. 😉
11th Jul 2021, 8:02 AM
David Carroll
David Carroll - avatar
+ 3
Jeison Last I checked... this doesn't exist in Java. However, it is available in Kotlin as "when expressions", which I much prefer over Java as a language that compiles to Java bytecode and runs in the JVM.
11th Jul 2021, 3:17 AM
David Carroll
David Carroll - avatar
+ 3
Java apparently have it https://docs.oracle.com/en/java/javase/16/language/switch-expressions.html BUT, your range bound had a floating point type (81.9), which is said to be "still on the table" here http://openjdk.java.net/jeps/325 And also referred to here https://nipafx.dev/java-13-switch-expressions/
11th Jul 2021, 4:57 AM
Ipang
+ 3
David, I didn't post it to compare Java against C# by feature.
11th Jul 2021, 6:07 AM
Ipang
+ 2
Beautiful !!!! Aaaaaaaa 😭 I'll like my code to be readable too so I'll keep that in mind
10th Jul 2021, 11:36 PM
Jace🎭
Jace🎭 - avatar
+ 1
It sure is interesting, I was first looking for a way to do it with switch till I stumbled upon ternary operators, but I'll check if something like that is possible with switch in Java 👍
11th Jul 2021, 3:02 AM
Jace🎭
Jace🎭 - avatar
+ 1
on the other side, Kotlin does not have "real" ternary operator: as Python, it have only onlined if..else shortcut, wich are more verbose than " ? : " ^^ in fact, as human, no language is perfect :D
11th Jul 2021, 3:24 AM
visph
visph - avatar
+ 1
David Carroll from my point of view both makes sense... Kotlin approach is obviously more near to "classic" ternary, but Python approach is more human readable (I think though... maybe because I discover first the Python way, and I'm not fan of Java/Kotlin ^^)
11th Jul 2021, 5:26 AM
visph
visph - avatar
0
I'm looking into it, I'm into backend development for now so let's see how it looks, it kinda makes me think about TypeScript, I still know little so thanks for the help and suggestions guys😁
11th Jul 2021, 3:44 AM
Jace🎭
Jace🎭 - avatar