Skip to main content
St Louis

Back to all posts

How to Declare A Variable In Kotlin?

Published on
4 min read
How to Declare A Variable In Kotlin? image

Best Kotlin Programming Books to Buy in September 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
7 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
+
ONE MORE?

In Kotlin, you can declare a variable using the val or var keywords.

  • When declaring a variable using val, it means the variable is read-only, or immutable. Once assigned a value, it cannot be reassigned. Example: val name = "Kotlin"
  • When declaring a variable using var, it means the variable is mutable, and its value can be changed after declaration. Example: var count = 10
  • Kotlin is a statically typed language, so you can explicitly specify the type of a variable when declaring it. However, type inference is also supported, allowing the compiler to automatically determine the type based on the assigned value. Example: val PI: Double = 3.14 var message: String = "Hello"
  • You can declare variables without explicitly specifying the type, and let the compiler infer it based on the assigned value. Example: val age = 25 var height = 180.5
  • Variables can also be declared without an initial value. In such cases, you need to specify the type. Example: val result: Int var title: String
  • Kotlin also allows for declaring nullable variables. To denote a variable that can be assigned null, you need to specify the type followed by a question mark ?. Example: val name: String? var count: Int?
  • Variables in Kotlin are by default non-null. If you try to assign or access a null value to a non-null variable, a compilation error will occur. To overcome this, you need to use nullable types.

That's it! These are the various ways to declare variables in Kotlin.

What is the data type of a variable inferred as if not explicitly mentioned?

In most programming languages, the data type of a variable is inferred based on the value assigned to it at the time of declaration. This is known as "type inference."

For example, if you assign an integer value to a variable:

var age = 30;

The variable age will be inferred as an integer data type, even if the data type is not explicitly mentioned. The type inference mechanism in the programming language determines the appropriate data type based on the assigned value.

However, it's important to note that the rules for type inference can vary between different programming languages. Some languages have more strict type inference rules, where the type must be explicitly declared, while others have more flexible type inference mechanisms.

How to declare a variable as a function in Kotlin?

In Kotlin, you can declare a variable as a function using the function type syntax. Here's an example:

// Declare a variable as a function that takes two integers and returns their sum val sum: (Int, Int) -> Int = { a, b -> a + b }

// Use the variable as a function val result = sum(3, 4) println(result) // Output: 7

In this example, the sum variable is declared with the function type (Int, Int) -> Int, which represents a function that takes two Int parameters and returns an Int value. The function is defined using a lambda expression that adds the two parameters a and b.

You can also declare a function type as a type alias for reusability:

// Declare a type alias for a function that takes two integers and returns their sum typealias IntSumFunction = (Int, Int) -> Int

// Declare a variable of the type alias and assign it a lambda function val sum: IntSumFunction = { a, b -> a + b }

// Use the variable as a function val result = sum(3, 4) println(result) // Output: 7

Using a type alias helps in making the code more readable and allows you to reuse the same function type in multiple places.

How to assign a value to a variable after declaration in Kotlin?

In Kotlin, you can assign a value to a variable after its declaration by using the var keyword. Here's an example:

var myVariable: Int

// ... some other code here ...

myVariable = 10 // Assigning a value to the variable

// Now you can use the variable with its assigned value println(myVariable) // Output: 10

In this example, the variable myVariable is declared with the type Int but doesn't have an initial value assigned to it. Later in the code, you can assign a value to it using the assignment operator (=).