Kotlin - Variables
Kotlin - Variables
Kotlin - Variables
Variables are an important part of any programming. They are the names you give to
computer memory locations which are used to store values in a computer program and
later you use those names to retrieve the stored values and use them in your program.
Kotlin variables are created using either var or val keywords and then an equal sign = is
used to assign a value to those created variables.
Syntax
Following is a simple syntax to create two variables and then assign them different values:
Examples
Once a variable is created and assigned a value, later we can access its value using its
name as follows:
Open Compiler
fun main() {
var name = "Zara Ali"
var age = 19
println(name)
println(age)
}
When you run the above Kotlin program, it will generate the following output:
Zara Ali
19
Let's see one more example where we will access variable values using dollar sign $:
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 1/7
Page 2 of 7
Open Compiler
fun main() {
var name = "Zara Ali"
var age = 19
println("Name = $name")
println("Age = $age")
}
When you run the above Kotlin program, it will generate the following output:
Let's see one more example to display the variable values without using a dollar sign as
below:
Open Compiler
fun main() {
var name = "Zara Ali"
var age = 19
When you run the above Kotlin program, it will generate the following output:
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 2/7
Page 3 of 7
Open Compiler
fun main() {
var name = "Zara Ali"
var age = 19
println("Name = $name")
println("Age = $age")
When you run the above Kotlin program, it will generate the following output:
Open Compiler
fun main() {
val name = "Zara Ali"
val age = 19
println("Name = $name")
println("Age = $age")
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 3/7
Page 4 of 7
println("Name = $name")
println("Age = $age")
}
When you run the above Kotlin program, it will generate the following output:
Read-only vs Mutable
The Mutable variables will be used to define variables, which will keep charging their
values based on different conditions during program execution.
You will use Read-only variable to define different constant values i.e. the variables which
will retain their value throughout of the program.
Open Compiler
fun main() {
var name: String = "Zara Ali"
var age: Int = 19
println("Name = $name")
println("Age = $age")
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 4/7
Page 5 of 7
When you run the above Kotlin program, it will generate the following output:
Soon we will learn more about different data types available in Kotlin which can be used to
create different type of variables.
Kotlin variable names can contain letters, digits, underscores, and dollar signs.
Kotlin variables are case sensitive which means Zara and ZARA are two different
variables.
Kotlin variable can not have any white space or other control characters.
Kotlin variable can not have names like var, val, String, Int because they are
reserved keywords in Kotlin.
A - Kotlin variables are used to store the information during program execution.
C - Kotlin read-only variables are defined using val keyword where as mutable
variables are defined using var keyword.
Answer : D
Explanation
All the given three statements are correct for Kotlin variables.
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 5/7
Page 6 of 7
A - First Line
B - Second Line
C - Third Line
D - Last Line
Answer : D
Explanation
At the last line we are trying to change the value of a read-only variable, so it will
raise and exception at this line.
A - My Name
B - #MyName
C - %MyName
D - -MyName
Answer : E
Explanation
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 6/7
Page 7 of 7
As per given rules to define a variable name in Kotlin, all the give variable names
are invalid
Answer : D
Explanation
Last statement is not correct because we can not make use of a reserved keyword
to name a Kotlin variable. Rest of the statements are correct.
https://www.tutorialspoint.com/kotlin/kotlin_variables.htm 7/7