Declare Variables in Kotlin
On this page (16sections)
Introduction
Variables are named containers used to store data in a program. In Kotlin you declare a variable with one of two keywords - var for values that can change, and val for values that stay fixed. Kotlin can also infer the type automatically, so declarations stay short and readable. This tutorial covers the syntax, mutable vs immutable variables, top-level variables, and the common ways to declare them, each with a runnable example and its output.
Kotlin variables
In Kotlin, declaring a variable is slightly different from that of C, C++ and Java. Either var or val keyword is used for declaring variables.
var variable_name:data_type = value;
or
val variable_name:data_type = value;
for example
var x:Int = 10;
Here data_type and values are optional. If we did not declare the datatype for a variable, the compiler infers datatype for that variable.
Kotlin variable types
- Mutable variable
- Immutable variable
- Global Variables / Top-level Variables
Types of variable declaration
- Immediate assignment of Datatypes
- Compiler Inferred Declaration
- Deferred assignment Declaration
Mutable variables
The Mutable variables are declared with the var keyword. The value assigned to these variables can be changed later in the program like the regular variables in C, C++ and Java.
Mutable variables example program
// Mutable variables Kotlin example program
// Variable Kotlin Programs , Basic Kotlin Programs
fun main(args: Array<String>) {
// Declared with datatype
var x:Int = 100
// Compiler Inferred Declaration : `Int` type is inferred
var y = 200
println("X Value : $x")
println("Y Value : $y")
}
Sample Output
X Value : 100
Y Value : 200
Immutable variables
The Immutable variables are declared with the val keyword. Once assigned, these values cannot be changed like the constant variables in C, C++ and Java.
Immutable variables example program
// Immutable variables Kotlin example program
// Variable Kotlin Programs , Basic Kotlin Programs
fun main(args: Array<String>) {
// Declared with datatype
val x: Int = 100
// Compiler Inferred Declaration : `Int` type is inferred
val y = 200
// Deferred assignment Declaration : no initial value is provided
val z: Int
z = 300
println("X Value : $x")
println("Y Value : $y")
println("Z Value : $z")
}
Sample Output
X Value : 100
Y Value : 200
Z Value : 300
Global Variables / Top-level Variables
In Kotlin, A global variable is a variable that is declared at the top of the program and outside all functions similar to C and C++. A local variable can only be used in the particular block where it is declared. A global variable can be used in all functions.
Global variables example program
// Global Variables / Top-levelVariables Kotlin example program
// Variable Kotlin Programs , Basic Kotlin Programs
// Declared Global Variable `Int` type is inferred
var x = 100
fun fn() {
x = x + 100
}
fun main(args: Array<String>) {
println("X Value : $x")
fn()
println("X Value : $x")
}
Sample Output
X Value : 100
X Value : 200
Best Practices
- Prefer
valovervar. Declare everythingvalby default and only switch tovarwhen a value genuinely needs to change. Immutable values make code easier to reason about. - Let the compiler infer simple types. For an immediate assignment such as
val count = 10, the explicit: Intis redundant. - Add an explicit type for deferred assignments. When you declare first and assign later (
val z: Int), the type is required. - Use clear, descriptive names in
camelCase, e.g.userAgerather thanx.
Common Mistakes
- Reassigning a
val.val x = 1; x = 2will not compile - usevarif you need to reassign. - Using a variable before assignment. A deferred
val z: Intmust be assigned before it is read. - Adding semicolons. Semicolons are optional in Kotlin; the trailing
;shown in some syntax examples is not required.
Frequently Asked Questions
What is the difference between var and val in Kotlin?
var declares a mutable variable whose value can be reassigned later. val declares a read-only variable that can be assigned only once - similar to a constant reference.Do I have to specify the data type when declaring a Kotlin variable?
var y = 200 is inferred as Int). You only need an explicit type for a deferred assignment.Can I change the value of a val variable?
val is assigned it cannot be reassigned. Prefer val by default and switch to var only when the value genuinely needs to change.