Variables Declaration
While writing programs in Kotlin, one can follow two different types of variable declarations.
Mutable variable
It denotes those variables whose values can be changed after their declaration. Mutable variables are declared using the keyword “var”. This keyword denotes that the value of this variable may change in the subsequent program. For example,
// Kotlin program to demonstrate mutable variables
fun main(){
var salary = 5000
println("Old salary: $salary")
// Salary doubles ... Increment !!!
salary *= 2
println("New salary: $salary")
}
Output:
Old salary: 5000
New salary: 10000
Immutable variables
It denotes those variables whose values can't be modified after their declaration. Immutable variables are declared using the keyword “val”. This keyword denotes that we cannot change the value of this variable in the following program. For example,
// Kotlin program to demonstrate immutable variables
fun main(){
val gravity = 9.8 // It is a constant value, so it must be declared using the val keyword
gravity = 4.6 // This will throw error
}
Output:
error: Val cannot be reassigned
Kotlin Basic Variable Types
The variables types are known at compile time as Kotlin is a statically typed language. The in-built data types can be categorized into the following categories:
Number Type
Numbers in Kotlin are similar to almost all modern-day programming languages. There are six in-built data types representing numbers. They are listed as follows:
-
Byte
The data type Byte can hold values in the range [-128,127].
-
Short
The data type Short can hold values in the range [-32768,32767].
-
Int
The data type Int can hold values in the range [-2^31, 2^31-1].
-
Long
The data type Long can hold values in the range [-2^63,2^63-1].
-
Float
The data type Float can hold single-precision 32-bit floating-point numbers.
-
Double
The data type Double can hold double-precision 64-bit floating-point numbers.
Character Type
The data type Char is used to represent a character in Kotlin. The following program illustrates Char data type in Kotlin.
// Kotlin program to demonstrate Char data type
fun main(){
val ch: Char = 'a'
println("The character is: $ch")
}
Output:
The character is: a
Boolean Type
The data type Boolean is used to store a true or false value in Kotlin. The following program illustrates Boolean data type in Kotlin.
// Kotlin program to demonstrate Boolean data type
fun main(){
var flag: Boolean = false
println("The variable flag is: $flag")
flag = true
println("The variable flag is updated to: $flag")
}
Output:
The variable flag is: false
The variable flag is updated to: true
Kotlin Strings
Strings are one of the basic data types in any programming language. Strings in Kotlin are represented using the String class. The String class in Kotlin is used to denote character strings. For example,
// Kotlin program to demonstrate String class
fun main(){
var company: String = "Coding Ninjas"
println("Welcome to $company")
}
Output:
Welcome to Coding Ninjas
For further reading about Strings in Kotlin, refer to the Kotlin Strings blog on the Coding Ninjas website.
Kotlin Arrays
Arrays in Kotlin are represented using the Array class. An array is a group of data of one single data type. For example, one can create an array of characters, strings, floats, etc.
Displaying the Variables
Variables can be displayed on the output screen/monitor using Kotlin in-built functions such as print() and println(). For example, see the following program that shows the value of a variable on the output screen.
// Kotlin program to demonstrate displaying the variables on console screen
fun main(){
var website_name = "Code Studio"
println("$website_name is the best platform to learn Computer Science fundamentals.")
}
Output:
Code Studio is the best platform to learn Computer Science fundamentals.
Frequently Asked Questions
-
Can we use variables in Kotlin without declaring them first?
No, Kotlin is a statically typed language. Therefore, it requires all variables to be declared before using them.
-
Can we reassign the value of a variable in Kotlin?
It depends upon the type of the variable. If the variable is declared using the "var" keyword, it can be reassigned, whereas if the variable is declared using the keyword “val”, it cannot be reassigned.
-
Can I start a variable name with a digit in Kotlin?
No, one cannot start a variable name with a digit. The variable names should begin with a letter, $ or _ symbol.
Key Takeaways
Cheers if you reached here!! The purpose of this blog was to introduce you to the concept of variables and study the various in-built data types present in Kotlin. To read about the input/output of variables in Kotlin, check out our Kotlin I/O blog.
Yet there is never an end to the learning process, so check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development and how to design the applications of the future. Until then, good luck!!