822

What is the equivalent of this expression in Kotlin?

a ? b : c

This is not valid code in Kotlin.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 9
    Official discussion: https://discuss.kotlinlang.org/t/ternary-operator/2116 – Vadzim Jun 17 '17 at 14:18
  • 1
    Maybe the showing the assignment would make this more clear. "d = (a) ? b : c" is now "d = if (a) b else c. Same works for when: "d = when { a -> b; c -> x; else ->d }". Personally I prefer the java/c/PERL syntax. – steven smith Jan 01 '21 at 21:42
  • Yep, the good designers of kotlin threw away the most elegant construct in java, replacing it with something that's 1) less readable, 2) harder to type (ie more "boilerplate code"), and 3) harder to understand for beginners (conflating expressions with statements). I think they changed the ternary because...it was java, which by their definition *has* to be bad. – SMBiggs Jan 31 '21 at 14:42
  • Using if-else expression in one single line statement is like ternary operator in Java. Kotlin does not support any ternary operator. – Vivek Hande Feb 23 '21 at 11:10
  • I am working with both C# and Kotlin already for years and I am still missing the `? :` operator in Kotlin. It's just more elegant, shorter and better to read than `if () else` construct. That's why this question has so high rating. – mojmir.novak May 13 '21 at 09:04
  • perl's implementation of the ternary operator allowed code to be included. For example: x = (some condition) ? doThis(): doThat(); Kotlin's Elvis Operator is a null check, only. – VikingGlen Sep 08 '21 at 04:31

34 Answers34

1083

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

if (a) b else c

The distinction between expression and statement is important here. In Java/C#/JavaScript, if forms a statement, meaning that it does not resolve to a value. More concretely, you can't 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 if is a statement, this might seem unnatural but that feeling should soon subside.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 78
    Additionally you can use `when`. – bashor May 02 '13 at 14:59
  • 10
    just to add, if it's a boolean expression, you can even go with `x = a==b` – gnomeria May 08 '17 at 16:29
  • 21
    Special mention for `var v = a ?: b`. This is same as `var v = if(a != null) a else b` – Deep Shah Jan 10 '18 at 04:16
  • 2
    Having Elvis operator, but not Ternary, is a sheer stupidity. However, this could have been rectified a little by adapting pythonic way of doing ternary, `b if (a) else c`, instead of, `if (a) b else c`. Btw, I'm not a fan of that either. – Adeel Ansari Feb 07 '18 at 11:33
  • 7
    @AdeelAnsari No, it is not rectifying. It is worse. Compare this. `b + if (a) c else d` vs. `b + (c if (a) else d)` The latter one requires additional parentheses. because `c` is not enclosed by the condition and `else`. – Naetmul Mar 12 '18 at 04:46
  • 2
    The Pythonic way is more English-ish, but far less mathematical. It requires additional syntax structure. Currently, every `if` statement or `when` statement is actually an *expression*, so *it just works* as the ternary operator. However, `b if (a) else c` does not just work as it is. It conflicts the fact that a `if` statement is actually an expression if we remove `if (a) b else c` and add `b if (a) else c`. It also breaks the consistency with `when`. – Naetmul Mar 12 '18 at 05:03
  • 4
    Here is a little discussion about this topic. https://discuss.kotlinlang.org/t/ternary-operator/2116/141 – F. Norbert Apr 18 '18 at 05:55
  • Kotlin coding guidelines prefers using `if` versus `when` on certain conditions as described here https://kotlinlang.org/docs/coding-conventions.html#if-versus-when – daparic Mar 21 '21 at 05:13
  • Reminds me of Ada's 'if-expression' https://learn.adacore.com/courses/intro-to-ada/chapters/imperative_language.html#if-expressions – chikega Nov 06 '22 at 22:21
  • poor design choice.. but of the whole language this is the only other poor design choice I can think of other than the whopper of making default access public wtf – ycomp Mar 05 '23 at 10:12
168

TL;DR

if (a) b else c

^ is what you can use instead of the ternary operator expression a ? b : c which Kotlin syntax does not allow.


In Kotlin, many control statements, such as if, when, and even try, can be used as expressions. As a result, these statements can have a result which may be assigned to a variable, be returned from a function, etc.

Syntactically, there's no need for ternary operator

As a result of Kotlin's expressions, the language does not really need the ternary operator.

if (a) b else c

is what you can use instead of the ternary operator expression a ? b : c.

I think the idea is that the former expression is more readable since everybody knows what ifelse does, whereas ? : is rather unclear if you're not familiar with the syntax already.

Nevertheless, I have to admit that I often miss the more convenient ternary operator.


Other Alternatives

when

You might also see when constructs used in Kotlin when conditions are checked. It's also a way to express if-else cascades in an alternative way. The following corresponds to the OTs example.

when(a) {
    true -> b
    false -> c
}

Extensions

As many good examples (Kotlin Ternary Conditional Operator) in the other answers show, extensions can also help with solving your use case.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
103

You could define your own Boolean extension function that returns null when the Boolean is false to provide a structure similar to the ternary operator:

infix fun <T> Boolean.then(param: T): T? = if (this) param else null

This would make an a ? b : c expression translate to a then b ?: c, like so:

println(condition then "yes" ?: "no")

Update: But to do some more Java-like conditional switch you will need something like that

infix fun <T> Boolean.then(param: () -> T): T? = if (this) param() else null

println(condition then { "yes" } ?: "no") pay attention on the lambda. its content calculation should be postponed until we make sure condition is true

This one looks clumsy, that is why there is high demanded request exist to port Java ternary operator into Kotlin

Mibac
  • 8,990
  • 5
  • 33
  • 57
deviant
  • 3,539
  • 4
  • 32
  • 47
58

Java's equivalent of ternary operator

a ? b : c

is a simple IF in Kotlin in one line

if(a) b else c

there is no ternary operator (condition ? then : else), because ordinary if works fine in this role.

https://kotlinlang.org/docs/reference/control-flow.html#if-expression


Special case for Null comparison

you can use the Elvis operator

if ( a != null ) a else b
// equivalent to
a ?: b
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
45

Some corner cases not mentioned in other answers.

Since appearance of takeIf in Kotlin 1.1 the ternary operator a ? b : c can also be expressed like this:

b.takeIf { a } ?: c

This becomes even shorter in case c is null:

b.takeIf { a }

A general workaround to make b lazily-evaluated:

true.takeIf { a }?.let { b } ?: c

Also note that typical in Java world null checks like value != null ? value : defaultValue translate in ideomatic Kotlin to just value ?: defaultValue.

Similar a != null ? b : c can be translated to a?.let { b } ?: c.


See is also Boolean?.ifTrue proposal in KT-6938 which should allow to streamline a ? b : null expressions to a.ifTrue { b } without prematurely evaluating the b.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • 11
    How is `b.takeIf { a } ?: c` shorter and more readable than `if (a) b else c`? Terneray operator is certainly a missing feature in Kotlin since variable names and the condition can be long and make you split the line which is bad – Javad Jul 08 '17 at 14:16
  • 3
    It should also be noted that `takeIf` always evaluates the true-case (here `a`). Not only may that expression be computed uselessly if `a` happens to be false, but you can't benefit from smart casts à la `if (a is Int) { a + 3 }`. – TheOperator Aug 09 '18 at 20:29
  • 1
    @TheOperator, wrong. `{ a }` is a lazily evaluated lambda. – Vadzim Aug 11 '18 at 12:08
  • 5
    I wrote it wrong, should be "always evaluates the true-case (here `b`)". But even `{ a }`, while lazy, _must_ be evaluated to determine the result of the expression. – TheOperator Aug 13 '18 at 06:43
  • 2
    `b.takeIf { a }` is technically equivalent to `b = a` – Jhourlad Estrella Dec 09 '20 at 06:01
  • Use `a?.let{b}?:c` instead of takeIf to avoid evaluating b. Also reads more like the ternary operator – Justin Rowe Aug 17 '21 at 11:46
37

For myself I use following extension functions:

fun T?.or<T>(default: T): T = if (this == null) default else this 
fun T?.or<T>(compute: () -> T): T = if (this == null) compute() else this

First one will return provided default value in case object equals null. Second will evaluate expression provided in lambda in the same case.

Usage:

1) e?.getMessage().or("unknown")
2) obj?.lastMessage?.timestamp.or { Date() }

Personally for me code above more readable than if construction inlining

ruX
  • 7,224
  • 3
  • 39
  • 33
  • 41
    It's not that relevant to the question, but why not use __?:__, the _elvis operator_? The first function would be replaced with `e.getMessage() ?: "unknown"`. The second can be expressed as `obj?.lastMessage?.timestamp ?: { Date() }()` – hotkey Jul 08 '15 at 21:09
  • 2
    @hotkey there is no special purpose for that. From my point of view it's looks more consistent and visually less noisy in chain operations as you shouldn't wrap construction in the brackets – ruX Jul 09 '15 at 11:15
  • 19
    @ruX the elvis operator is specifically for this and your use is rather unusual. – Jayson Minard Dec 29 '15 at 18:49
  • 7
    While ?: is fine, let's not go too far down the road to Perl. – Richard Haven Mar 01 '16 at 16:14
37

There is no ternary operator in kotlin, as the if else block returns the value.

so, you can do: val max = if (a > b) a else b instead of java's max = (a > b) ? b : c

We can also use when construction, it also returns value:

val max = when(a > b) {
    true -> a
    false -> b
}

Here is the link for kotlin documentation : Control Flow: if, when, for, while

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
romiope
  • 756
  • 8
  • 11
34

if condition is false then "wrong", otherwise it's "someString"

Let's consider the following prototype before writing a ternary conditional operator:

if (!answer.isSuccessful()) {
    result = "wrong"
} else {
    result = answer.body().string()
}

return result


Solutions

You can use ! (logical not) operator in Kotlin's if-expression:

return if (!answer.isSuccessful()) "wrong" else answer.body().string()

It can be much better if you flip if-expression (expression without ! operator):

return if (answer.isSuccessful()) answer.body().string() else "wrong"

Kotlin’s Elvis operator ?: can do a job even better:

return answer.body()?.string() ?: "wrong"

Also, use an Extension function for the corresponding Answer class:

fun Answer.bodyOrNull(): Body? = if (isSuccessful()) body() else null

In the Extension function you can reduce a code thanks to Elvis operator:

return answer.bodyOrNull()?.string() ?: "wrong"

Or just use when conditional expression:

when (!answer.isSuccessful()) {
    parseInt(str) -> result = "wrong"
    else -> result = answer.body().string()
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • I'm a few tens of hours into my Kotlin journey, and I am surprised how often an extension method is the right answer. Often, I find I can't use the `?.` and `?:` operators because I'm wanting to pass the `Type?` to a method elsewhere, rather than invoke a method on *it*. (eg. pass a `String?` to `DateTimeFormatter. for parsing). In that case, we're back to an `if (a != null) DateTimeFormatter.parse(s)`. But quick extension method 'turns it around' to `s?/toSpecialDate()` and almost always helps shove details out of the flow of the logic! I am loving Kotlin :-) – David Bullock Nov 18 '20 at 09:59
31

In Kotlin, if is an expression, i.e. it returns a value. Therefore there is no ternary operator (condition ? then : else), because ordinary if works fine in this role. manual source from here

// Traditional usage 
var max = a 
if (a < b) max = b

// With else 
var max: Int
if (a > b) {
    max = a
} else {
    max = b
}

// As expression 
val max = if (a > b) a else b
LF00
  • 27,015
  • 29
  • 156
  • 295
27

Take a look at the docs:

In Kotlin, if is an expression, i.e. it returns a value. Therefore there is no ternary operator (condition ? then : else), because ordinary if works fine in this role.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Li Ying
  • 2,261
  • 27
  • 17
21

Another interesting approach would be to use when:

when(a) {
  true -> b
  false -> c
}

Can be quite handy in some more complex scenarios. And honestly, it's more readable for me than if ... else ...

sebnukem
  • 8,143
  • 6
  • 38
  • 48
Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
18

Java

int temp = a ? b : c;

Equivalent to Kotlin:

var temp = if (a) b else c
Elias Mårtenson
  • 3,820
  • 23
  • 32
doubleThunder
  • 264
  • 2
  • 5
16

Kotlin doesn't have a ternary operator. You can use the regular if expression as shown below:

if (condition) exp1 else exp2

Also in addition to the fact that if in Kotlin is not a statement but an expression (i.e. it evaluates to a value), in the case where you have multiple statements inside the body of an if branch (or the body of else or else if), the last line of the block is the value of that branch. For example:

if (a>b) {
    println("a is greater than b")
    a // value of this if
} else {
    println("b is greater than a")
    b // value of else
}
krishnakeshan
  • 1,242
  • 2
  • 14
  • 18
15

There is no ternary operator in Kotlin. It seems problematic at the first glance. But think we can do it with inline if else statement because this is expression here. Simply we have to do -

var number = if(n>0) "Positive" else "Negetive"

Here we can else if block too as many as we need. Like-

var number = if(n>0) "Positive" else if(n<0) "Negative" else "Zero"

So this line is so simple and much readable than ternary operator. when we use more than one ternary operator in java it seems horrible. But here we have a clear syntax. even we can write it in multiple line too.

HM Nayem
  • 2,287
  • 1
  • 12
  • 10
15

You can use var a= if (a) b else c in place of the ternary operator.

Another good concept of kotlin is the Elvis operator. You don't need to check null every time.

val l = b?.length ?: -1

This will return length if b is not null otherwise it executes the right side statement.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Android Geek
  • 596
  • 8
  • 14
13

when replaces the switch operator of C-like languages. In the simplest form it looks like this

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> {
        print("x is neither 1 nor 2")
    }
}
Guruprasath
  • 152
  • 1
  • 5
  • 4
    True, but the example you show has `when` as a statement, not an expression. A more relevant comparison with ternary conditional expressions would be to have each branch return a value, such that the entire when expression evaluates to a value (as happens with ternary conditionals). – Drew Noakes May 23 '17 at 14:08
11

as Drew Noakes quoted, kotlin use if statement as expression, so Ternary Conditional Operator is not necessary anymore,

but with the extension function and infix overloading, you could implement that yourself, here is an example

infix fun <T> Boolean.then(value: T?) = TernaryExpression(this, value)

class TernaryExpression<out T>(val flag: Boolean, val truly: T?) {
    infix fun <T> or(falsy: T?) = if (flag) truly else falsy
}

then use it like this

val grade = 90
val clazz = (grade > 80) then "A" or "B"
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Minami
  • 963
  • 6
  • 21
  • Maybe remove better?infix fun or(falsy: T?) = if (flag) truly else falsy – solo Mar 22 '18 at 03:14
  • 2
    But add can make it work: (grade > 80) then null or "B" – solo Mar 22 '18 at 03:59
  • This is really cool, I'm going to use it :P But do note that, unless I'm mistaken, it'll cause an object allocation every single time it's called. Not a huge deal, but worth knowing it's not a zero cost abstraction. – Adam Jun 17 '19 at 18:36
  • There could be unwanted side-effect because both `truly` and `falsy` must be evaluated. Writing `(grade > 80) then a() or b()` will invoke both `a()` and `b()`, but `if (grade > 80) a() else b()` will invoke either `a()` or `b()`, but not both. – tia Sep 21 '22 at 13:13
11

You can do something like this:

val ans = (exp1 == exp2) then "yes" ?: "no"

by using this extension:

infix fun<T> Boolean.then(first: T): T? = if (this) first else null

P.S: Don't modify the above infix function to accept first: T?, the expression will become logically incorrect. Eg: If you modify it to accept nullable first: T?, then val ans = (true == true) then null ?: "abcd", ans will be "abcd", which would not be correct.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Anirudh Gupta
  • 573
  • 5
  • 12
  • Inspired by your answer, I came up with this: https://pl.kotl.in/tB7xXs6lR. Of course, the performance would be bad as it creates an intermediate object. – Yihao Gao Mar 23 '21 at 11:05
9

You can do it many way in Kotlin

  1. Using if

    if(a) b else c
    
  2. Using when

    when (a) { 
        true -> print("value b") 
        false -> print("value c") 
        else -> {  
            print("default return in any other case") 
        } 
    }
    
  3. Null Safety

    val a = b ?: c
    
Elias Mårtenson
  • 3,820
  • 23
  • 32
Rajesh Dalsaniya
  • 1,977
  • 1
  • 13
  • 12
9

There is no ternary operation in Kotlin, but there are some fun ways to work around that. As others have pointed out, a direct translation into Kotlin would look like this:

val x = if (condition) result1 else result2

But, personally, I think that can get a bit cluttered and hard to read. There are some other options built into the library. You can use takeIf {} with an elvis operator:

val x = result1.takeIf { condition } ?: result2

What is happening there is that the takeIf { } command returns either your result1 or null, and the elvis operator handles the null option. There are some additional options, takeUnless { }, for example:

val x = result1.takeUnless { condition } ?: result2

The language is clear, you know what that's doing.

If it's a commonly used condition, you could also do something fun like use an inline extension method. Let's assume we want to track a game score as an Int, for example, and we want to always return 0 if a given condition is not met:

inline fun Int.zeroIfFalse(func: () -> Boolean) : Int = if (!func.invoke()) 0 else this     

Ok, that seems ugly. But consider how it looks when it is used:

var score = 0
val twoPointer = 2
val threePointer = 3

score += twoPointer.zeroIfFalse { scoreCondition } 
score += threePointer.zeroIfFalse { scoreCondition } 

As you can see, Kotlin offers a lot of flexibility in how you choose to express your code. There are countless variations of my examples and probably ways I haven't even discovered yet. I hope this helps!

pranalli
  • 91
  • 1
  • 4
7

Remember Ternary operator and Elvis operator hold separate meanings in Kotlin unlike in many popular languages. Doing expression? value1: value2 would give you bad words by the Kotlin compiler, unlike any other language as there is no ternary operator in Kotlin as mentioned in the official docs. The reason is that the if, when and try-catch statements themselves return values.

So, doing expression? value1: value2 can be replaced by

val max = if (a > b) print("Choose a") else print("Choose b")

The Elvis operator that Kotlin has, works only in the case of nullable variables ex.:

If I do something like value3 = value1 ?: value2 then if value1 is null then value2 would be returned otherwise value1 would be returned.

A more clear understanding can be achieved from these answers.

Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
6

If you do not what to use the standard notation you can also create/simulate it using infix with something like this:

create a class to hold your target and result:

data class Ternary<T>(val target: T, val result: Boolean)

create some infix functions to simulate a ternary operation

infix fun <T> Boolean.then(target: T): Ternary<T> {
    return Ternary(target, this)
}

infix fun <T> Ternary<T>.or(target: T): T {
    return if (this.result) this.target else target
}

Then you will be able to use it like this:

val collection: List<Int> = mutableListOf(1, 2, 3, 4)

var exampleOne = collection.isEmpty() then "yes" or "no"
var exampleTwo = (collection.isNotEmpty() && collection.contains(2)) then "yes" or "no"
var exampleThree = collection.contains(1) then "yes" or "no"
Eudy Contreras
  • 396
  • 3
  • 10
4

You can use the if expression for this in Kotlin. In Kotlin if is an expression with a result value. So in Kotlin, we can write

fun max(a: Int, b: Int) = if (a > b) a else b

and in Java, we can achieve the same but with larger code

int max(int a, int b) {
return a > b ? a : b
}
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Gulzar Bhat
  • 1,255
  • 11
  • 13
2

Why would one use something like this:

when(a) {
  true -> b
  false -> b
}

when you can actually use something like this (a is boolean in this case):

when {
  a -> b
  else -> b
}
ZZ 5
  • 1,744
  • 26
  • 41
  • 2
    Because the first one is semantically clear & easily understandable to someone else reading it even if they're not familiar w/Kotlin, while the 2nd one is not. – mc01 Sep 16 '18 at 02:43
  • 2
    Well, you've got the point, however I can't understand why Kotlin developers didn't introduce ternary expression – ZZ 5 Sep 16 '18 at 07:42
  • I think `? and :` contradicts with the nullable/ type declaration rather than a type check. Apart from it I don't see any reason. I think someone would have definitely put some thought, if there is inline if-else condition check. Let's wait and see in future versions. – bh4r4th Feb 04 '20 at 02:40
2

Other answers here with e.g. takeIf assume something like x = (a != null) ? a : c or x = (a != null) ? a.method() : c but this is not in fact equivalent to x = a ? b : c

To achieve a true equivalent of the ternary operator with null safety-check (i.e. x = (a != null) ? b : c) you can use let instead of takeIf, i.e.

val x = a?.let {b} ?: c

where b may or may not be a method that invokes something on a.

Justin Rowe
  • 2,356
  • 1
  • 20
  • 15
1

Another short approach to use

val value : String = "Kotlin"

value ?: ""

Here kotlin itself checks null value and if it is null then it passes empty string value.

Vinod Pattanshetti
  • 2,465
  • 3
  • 22
  • 36
1

When working with apply(), let seems very handy when dealing with ternary operations, as it is more elegant and give you room

val columns: List<String> = ...
val band = Band().apply {
    name = columns[0]
    album = columns[1]
    year = columns[2].takeIf { it.isNotEmpty() }?.let { it.toInt() } ?: 0
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Juan Mendez
  • 2,658
  • 1
  • 27
  • 23
1

There is no ternary operator in Kotlin, the most closed are the below two cases,

  • If else as expression statement

val a = true if(a) print("A is true") else print("A is false")

  • Elvis operator

If the expression to the left of ?: is not null, the elvis operator returns it, otherwise it returns the expression to the right. Note that the right-hand side expression is evaluated only if the left-hand side is null.

 val name = node.getName() ?: throw IllegalArgumentException("name expected")

Reference docs

JTeam
  • 1,455
  • 1
  • 11
  • 16
1

In Kotlin you can use ternary operation like this: val x = if(a) "add b" else "add c"

  • 2
    This question has been answered already enough, and has not been updated recently. There's no need to post now another answer that doesn't differ from earlier ones. – Headcracker May 26 '20 at 10:43
1

In Java, if is a statement but, in Kotlin if is an expression. It is called an expression because it compares the values of a and b and returns the maximum value. Therfore, in Kotlin there is no ternary operator (a>b)?a:b because it is replaced by the if expression.

Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61
0

With the following infix functions I can cover many common use cases pretty much the same way it can be done in Python :

class TestKotlinTernaryConditionalOperator {

    @Test
    fun testAndOrInfixFunctions() {
        Assertions.assertThat(true and "yes" or "no").isEqualTo("yes")
        Assertions.assertThat(false and "yes" or "no").isEqualTo("no")

        Assertions.assertThat("A" and "yes" or "no").isEqualTo("yes")
        Assertions.assertThat("" and "yes" or "no").isEqualTo("no")

        Assertions.assertThat(1 and "yes" or "no").isEqualTo("yes")
        Assertions.assertThat(0 and "yes" or "no").isEqualTo("no")

        Assertions.assertThat(Date() and "yes" or "no").isEqualTo("yes")
        @Suppress("CAST_NEVER_SUCCEEDS")
        Assertions.assertThat(null as Date? and "yes" or "no").isEqualTo("no")
    }
}

infix fun <E> Boolean?.and(other: E?): E? = if (this == true) other else null
infix fun <E> CharSequence?.and(other: E?): E? = if (!(this ?: "").isEmpty()) other else null
infix fun <E> Number?.and(other: E?): E? = if (this?.toInt() ?: 0 != 0) other else null
infix fun <E> Any?.and(other: E?): E? = if (this != null) other else null
infix fun <E> E?.or(other: E?): E? = this ?: other
Nicolas Cornette
  • 772
  • 7
  • 11
0

We can use ifEmpty function also just like below.

 val userName="anystring"
 textview.text = userName.ifEmpty { "otherstring" }
Soni Kumar
  • 283
  • 1
  • 4
  • 16
-1

example: var energy: Int = data?.get(position)?.energy?.toInt() ?: 0

In kotlin if you are using ?: it will work like if the statement will return null then ?: 0 it will take 0 or whatever you have write this side.

abhilasha Yadav
  • 217
  • 1
  • 9
-1

fun max(x:Int,y:Int) : String = if (x>y) "max = $x" else "max = $y"

inline funcation