Syntax

Kotlin Safe Call and Elvis Syntax

In Kotlin, safe call and elvis: handles null safely with a default — val len = name?.length ?: 0

Quick Answer

Syntax
val len = name?.length ?: 0
KotlinSyntax

Safe Call and Elvis

Code example
val len = name?.length ?: 0

Handles null safely with a default.

Learn More

How to safe call and elvis in Kotlin

  1. Open your Kotlin file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Kotlin Safe Call and Elvis Syntax

Use val len = name?.length ?: 0.

Related Syntax References

Frequently asked questions

What is the Kotlin syntax for safe call and elvis?

Use this pattern: val len = name?.length ?: 0

Can I copy this Kotlin code example?

Yes. Use Copy code and adapt the example to your program.

Is this Kotlin syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify