How to Use Java Ternary Conditional Operator in Kotlin

In this article, we will learn about how to use Java Ternary Conditional Operator in Kotlin.

In Kotlin, if statements are expressions. So the following code is equivalent.

if (a) b else c

The distinction between expression and utterance is important here. In Java/C#/JavaScript, it forms an instruction, which means it is not resolved in value. Specifically, you cannot assign it to a variable.

// Valid Kotlin, but invalid Java/C#/JavaScript
var v = if (a) b else c

If you’re coming from a language where this is a statement it might sound unnatural, but that feeling should wear off soon.

Next Article

1. How to Iterate Through Java MAP in Android

Please share this post:
Posts created 64

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: