codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

10 / 68 topics
1Introduction to Kotlin2Kotlin Installation3Hello World Program4Variables and Data Types5Operators in Kotlin6Control Flow Statements7Functions in Kotlin8Strings and Templates9Collections in Kotlin10Null Safety
Tutorials/Kotlin/Null Safety
🎯Kotlin

Null Safety

Updated 2026-04-20
3 min read

Null Safety

Null safety is a core feature of Kotlin that helps developers write safer and more robust code by eliminating the risk of null pointer exceptions (NPEs). Unlike Java, which allows variables to be explicitly set to null, Kotlin distinguishes between nullable and non-nullable types. This distinction ensures that you can't accidentally pass a null value where it's not expected.

Understanding Null Safety

In Kotlin, every type is non-null by default. If you want to declare a variable that can hold a null value, you need to explicitly mark it as nullable using the ? symbol.

Non-Nullable Types

Non-nullable types are the default in Kotlin. They cannot hold null values and provide safety against NPEs.

var name: String = "John"
// name = null  // This will cause a compilation error

Nullable Types

Nullable types can hold either a non-null value or null. You must explicitly declare them with the ? symbol.

var nullableName: String? = "John"
nullableName = null  // This is allowed

Safe Calls and Elvis Operator

Kotlin provides several operators to handle nullable types safely.

Safe Call Operator (?.)

The safe call operator allows you to call methods or access properties on a nullable object without causing an NPE. If the receiver of the safe call is null, the expression returns null.

fun greet(name: String?) {
    println("Hello, ${name?.toUpperCase() ?: "Guest"}!")
}

greet(null)  // Output: Hello, Guest!

Elvis Operator (?:)

The Elvis operator provides a default value if the nullable expression is null.

val length = name?.length ?: 0

Not-Null Assertions

Sometimes you are certain that a variable will not be null at runtime. In such cases, you can use the not-null assertion operator (!!) to tell Kotlin to treat the value as non-null.

Caution: Use this operator with extreme care, as it can lead to NPEs if the assumption is incorrect.

val length = name!!.length  // Throws NullPointerException if name is null

Smart Casts

Kotlin's type system includes smart casts, which automatically cast variables when they are used in a way that eliminates the possibility of null.

fun printLength(name: String?) {
    if (name != null) {
        println("Name length: ${name.length}")  // name is automatically cast to non-null here
    }
}

Handling Nulls with let, apply, and run

Kotlin provides several scope functions that can be used to handle nullable values in a more functional style.

let

The let function executes a lambda on the object if it is not null and returns the result of the lambda. If the object is null, it returns null.

val length = name?.let { it.length }

apply

The apply function is used for configuring objects. It returns the object itself after executing the lambda.

val user = User().apply {
    this.name = "John"
    this.age = 30
}

run

The run function executes a block of code and returns its result. It is similar to let, but it does not require a safe call operator.

val length = name.run { this.length }

Best Practices

  1. Use Non-Nullable Types by Default: Only use nullable types when necessary.
  2. Avoid Not-Null Assertions (!!): Use them sparingly and only when you are absolutely sure the value is not null.
  3. Leverage Safe Calls and Elvis Operator: They help in writing concise and safe code.
  4. Use Scope Functions Wisely: They make your code more readable and functional.

Conclusion

Kotlin's null safety feature is a powerful tool that helps developers write safer and more reliable code. By understanding the distinction between nullable and non-nullable types, using safe calls, Elvis operator, and scope functions, you can effectively handle null values in Kotlin. This not only prevents runtime exceptions but also makes your code cleaner and easier to maintain.

By following best practices and leveraging Kotlin's type system, you can take full advantage of null safety and write high-quality applications.


PreviousCollections in KotlinNext Extensions Functions

Recommended Gear

Collections in KotlinExtensions Functions