Variables
are used to store a value in memory. There are two types of variables.
Memory allocation is dependent on the data type.
Mutable and Immutable. Mutable variables allow the modification of their value once assigned.
In Kotlin, Every data is stored as a Variable. Variables can be declared with Kotilin data types
How to declare a variable in Kotlin?
A variable declared with the following syntax
val/var variable_name[: datatype]=value
Val
and var
are keywords used to declare a variable, always start with a declaration
Val
is a constant declaration that contains a value, never changing once the variable is assigned a value.
var
is a variable that changes its value during program execution.
variable_name is a valid identifier.
Datatype
is an valid datatypes in kotlin
Datatype is optional, if omitted, the variable type is inferred on the type of the value assigned.
value is data assigned to a variable
Here are some variable declaration examples
fun main(){
// variable examples with or without datatype
var str = "john"
var str1: string = "mark"
// constant declaration using val
val str2 = "john"
val str3: string = "john"
}
var variable declaration examples
fun main(){
//variable declare, assigned with a value
var str = "eric"
println(str);
//variable reassigned with new value
str="mark"
println(str);
}
val
variable declaration examples.
val variables throw an error if changing its value and the error is Val cannot be reassigned
fun main(){
//constant declare, assigned with a value
val str = ""
println(str);
//variable reassigned with new value
str="mark" // throws an error Val cannot be reassigned
println(str);
}
Let another example variable be declared without assigning a value
fun main() {
val name
println(name)
}
It throws This variable must either have a type annotation or be initialized
How to declare multiple variables at once in Kotlin
Java supports multiple variable declarations and assigns the value.
Kotlin supports multiple variables declaration and assigns values
Here is an example
val age = 25; val name = "Eric";
The above declared two variables of different values assigned.
Each variable declaration ends with a semicolon ;
Similarly, multiple variables can be declared in a class as given below.
class Employee {
var age = 30; var name = "mark";
}
Check the Type of a variable in Kotlin
Variable type checked using different ways
is
operator: Checks a given variable is of a given type. This is used in if conditional check with a given datatype or not
Here is an example to check variable is of Int and String type in Kotlin
fun main() {
val name = "mark"
if(name is String) {
println("Given variable is String")
}
val number = 12
if(number is Int) {
println("Given variable is Int")
}
}
Output
Given variable is String
Given variable is Int
- Another way using reflection as a String
Kotlin provides reflection API to inspect bytecode. It provides the following properties and each returns a String.
variable::class.simpleName
: Returns datatype of simple datatypevariable::class.qualifiedName
: returns a Kotlin datatype with the package namevariable::class.java.typeName
: Returns java datatype
Here is an example
fun main() {
val name = "mark"
println(name::class.simpleName) // String
println(name::class.qualifiedName) // kotlin.String
println(name::class.java.typeName) // java.lang.String
val number = 12
println(number::class.simpleName) // Int
println(number::class.qualifiedName) // kotlin.Int
println(number::class.java.typeName) // int
}