Sling Academy
Home/Kotlin/How to Declare Variables in Kotlin

How to Declare Variables in Kotlin

Last updated: November 29, 2024

Kotlin is a modern programming language that is fully interoperable with Java and widely used for Android development. One of the fundamental aspects of programming in Kotlin is declaring variables. In this article, we will explore how to declare variables using the val and var keywords, understand the difference between them, and look at some examples.

Understanding val and var

In Kotlin, there are two types of variable declarations:

  • val - for variables that are read-only (immutable)
  • var - for variables that are writable (mutable)

The val Keyword

The val keyword is used to declare a read-only variable, which means its value cannot be changed once it is assigned. It is similar to the final keyword in Java, denoting a constant reference.

val immutableVariable: String = "I cannot be changed"
// immutableVariable = "Attempt to change" // This will cause a compile error

Once you assign a value to immutableVariable, its value cannot be reassigned, making it a helpful feature to ensure some data should not be altered after being initialized.

The var Keyword

The var keyword, on the other hand, is used for declaring mutable variables. You can change the value of these variables anytime after their initial assignment.

var mutableVariable: Int = 10
mutableVariable = 20
println(mutableVariable)  // This will output 20

Since mutableVariable is declared with var, its value can be updated freely.

Type Inference

Kotlin has a feature called type inference, which allows the compiler to determine the variable's type based on the assigned value, enabling you to declare variables without explicitly specifying their type:

val inferredString = "Kotlin is fun!"
var inferredInt = 123

In the above example, inferredString is automatically interpreted as a String, and inferredInt is recognized as an Int.

Nullable Variables

Kotlin handles nullability with a specific syntax, using the question mark (?) to denote that a variable can hold a null value.

var nullableVariable: String? = null
nullableVariable = "Now I have a value!"

Here, nullableVariable is explicitly declared nullable, meaning it can either hold string values or null.

Conclusion

Understanding how to declare variables in Kotlin with val and var allows you to manage data effectively in your applications. Knowing when to use immutable or mutable variables can help protect your data and prevent errors. Type inference makes your code cleaner, while nullable types enhance its safety. With these concepts, you're well on your way to effectively using Kotlin for development.

Next Article: Immutable Variables: The Power of `val`

Previous Article: Using Block and Inline Comments in Kotlin

Series: Basics of Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin