Skip to main content
infervour.com

Back to all posts

How to Declare Variables In Kotlin?

Published on
6 min read
How to Declare Variables In Kotlin? image

Best Kotlin Programming Books to Buy in October 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)
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
9 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
10 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
+
ONE MORE?

In Kotlin, you can declare variables using the var and val keywords.

The var keyword is used for mutable variables, which means their values can be changed throughout the program. For example:

var age: Int = 25 var name: String = "John"

In the above code, age and name are mutable variables of type Int and String respectively. The values assigned to them can be modified later in the program.

On the other hand, the val keyword is used for immutable variables, meaning their values cannot be changed once assigned. For example:

val pi: Double = 3.14159 val country: String = "USA"

In this code snippet, pi and country are immutable variables of type Double and String respectively. Once assigned, their values cannot be modified.

Kotlin also supports type inference, which means you can omit the type declaration and let the compiler infer it based on the assigned value. For example:

var quantity = 10 val price = 9.99

In the above code, quantity is inferred as Int, and price is inferred as Double.

You can also declare a variable without assigning an initial value, but in such cases, you must explicitly specify the variable type. For example:

var temperature: Float val isSunny: Boolean

Here, temperature is a mutable variable of type Float, and isSunny is an immutable variable of type Boolean. Their values need to be assigned later in the program before using them.

What is the purpose of type inference in Kotlin variables?

The purpose of type inference in Kotlin variables is to allow the compiler to automatically determine the type of a variable based on its initialization value. This eliminates the need for explicit type declarations, making the code more concise and readable. Type inference also helps to reduce redundancy and improve developer productivity by reducing the amount of code that needs to be written.

How do you create a variable in Kotlin?

To create a variable in Kotlin, you need to specify the variable name, the data type, and an optional initial value. Here's how you can create a variable in Kotlin:

  1. var keyword: The var keyword is used to declare a mutable variable, meaning its value can be changed later.

var variableName: DataType = initialValue

For example, to create a variable named age of type Int with an initial value of 25, you can write:

var age: Int = 25

  1. val keyword: The val keyword is used to declare an immutable variable, meaning its value cannot be changed once assigned.

val variableName: DataType = initialValue

For example, to create an immutable variable named pi of type Double with an initial value of 3.14, you can write:

val pi: Double = 3.14

In Kotlin, type inference is used by default, so you can omit the explicit data type declaration:

var age = 25 // inferred type: Int val pi = 3.14 // inferred type: Double

It is important to note that if you initialize the variable later in the code, you need to provide the data type explicitly.

How to declare variables with type aliases in Kotlin?

In Kotlin, you can declare variables with type aliases by following these steps:

  1. Define the type alias using the typealias keyword. This is usually done at the top level of your file or within a class or interface declaration. For example:

typealias Name = String

  1. Use the type alias when declaring a variable. You can use it just like any other data type. For example:

val username: Name = "John"

In this example, the variable username is declared with the type alias Name, which is equivalent to the String type.

Type aliases provide a simple way to create shortcuts for complex types or to give more meaningful names to existing types. They can be especially useful when dealing with long or generic type names.

Note that type aliases do not create new types, but rather provide alternative names for existing types. The underlying type of a variable declared with a type alias is the same as the type it aliases, and they are interchangeable in all respects.

The recommended naming convention for Kotlin variables follows the standard Java conventions. It is generally recommended to use camel case notation, where the variable name starts with a lowercase letter and the first letter of each subsequent concatenated word is capitalized.

Additionally, it is common to use descriptive names that accurately represent the purpose or content of the variable. This improves code readability and enhances understanding.

Here's an example of a recommended naming convention for Kotlin variables:

// For general variables val firstName: String = "John" var age: Int = 25

// For constants, use uppercase with underscore separating words val MAX_RETRIES: Int = 3

// For class properties, you can use camel case or use underscore for private properties class Person { val firstName: String = "John" private val _lastName: String = "Doe" }

Remember that following a consistent naming convention across your codebase helps improve code maintainability and readability.

How to declare a variable with a specific type in Kotlin?

In Kotlin, you can declare a variable with a specific type using the following syntax:

var variableName: DataType

or

val variableName: DataType

Here, var is used to declare a mutable variable, while val is used to declare an immutable (read-only) variable.

Replace variableName with the actual name you want to give to the variable, and DataType with the specific type you want to assign to it.

For example, if you want to declare a variable named age of type Int, you can do it like this:

var age: Int

Similarly, if you want to declare a read-only variable named name of type String, you can do it like this:

val name: String

Once you've declared a variable with a specific type, you can assign a value to it later using the = operator.

How to declare variables in Kotlin?

In Kotlin, variables can be declared using the 'val' or 'var' keyword.

  • 'val' is used to declare a read-only variable, also known as an immutable variable. Once assigned, its value cannot be changed. Syntax: val variableName: Type = value

Example: val name: String = "John"

  • 'var' is used to declare a mutable variable, where the value can be modified after the initial assignment. Syntax: var variableName: Type = value

Example: var age: Int = 25

Note that the type declaration can be omitted if the variable is initialized with a value, as Kotlin can infer the type from the assigned value.

Example: val message = "Hello, World!"

If the variable is not initialized with a value, its type must be explicitly declared.

Example: var count: Int