Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why there is no ternary operator in kotlin?

Kotlin implements if statement as an Expression, so it returns a value. Since if expression can return a value, we can achieve the Ternary (?) functionality using if expression.

TernaryDemo.kt
fun main(args: Array) {

var a : Int = 10
var b : Int = 20

val max = if (a > b) a else b

println("Maximum element is : $max")
}

Output
Maximum element is : 20


Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Why there is no ternary operator in kotlin?

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×