What are the variables in Kotlin?
Variables are the name of the data. Variables in Kotlin are used to store values. Variable is similar to the container. That is used to store data.
Need of variables in kotlin
To make the program more efficient.
Work with unknown data.
Type of Kotlin Variables
There are mainly two types of variables in Kotlin.
1. Var
2. Val
Val vs var Kotlin
1. Var
Var variables are mutable. Mutable means data store in var can be changed. You can easily change the data. In your program, if you want to change the data further. Then you need to use a var type of variable. You can easily re-assign the data.
Examples of Var
fun main() {
var count = 5
count =7
print(count)
}
Output
7
2. Val
Val variables are immutable. Immutable means data store in Val can not be changed. You can not change the data. In your program, if you do not want to change the data further. Then you need to use a Val type of variables. You can not re-assign the data.
Examples of Val
ffun main() {
val count = 5
count =7
print(count)
}
Output
Why we need variables whose data cannot be changed?
If the value of variables in Kotlin is continuously changing. Then the complexity of programs increases. Your code becomes difficult to understand. To reduce the complexity. We need to use the Val variables. With the use of Val variable values remain the same and fixed. The fixed value is easily traced by programmers.
Kotlin Data Types
Now the question arises, What is dataType in Kotlin? Basically, the data type tells us. What kinds of data we are going to use in the program. A data type is an attribute of data. It tells the compiler the type of data used by the programmer.
Type Inference
Kotlin is a Type Inference Data type. Now, what is type inference?
Kotlin automatically detects the data type. And tells the compiler about the data type.
Kotlin Data Types
Int(Whole numbers, both positive negative integer)
Double(decimal number)
Boolean(true or false)
Char(characters)
String(a sequence of characters)
Float(decimal numbers)
Int
We know that kotlin is a dynamically typed language. There is no need to specify a data type. Kotlin automatically detects data type. But users can explicitly define the data type. Kotlin Int is similar to other programming languages int. It is used to store the whole numbers and both positive negative integers.
Examples
Implicitly define
In this case, it automatically detects the data type is Int.
fun main() {
var count = 5
print(count)
}
Output
5
Explicitly define
In this case, users have to define the data type.
Now questions arise.
why we explicitly define data type?
In some cases, we declare an empty variable. And store value in it later. In this case, we have to assign the data type
Example
fun main() {
var count:Int = 5
print(count)
}
Output
5
Double(decimal number)
It is used to store the decimal values. When we need to calculate the percentage. Another decimal quantity. In that case, we used Double data type.
Examples
Implicitly define
In this case, it automatically detects the data type is Double.
fun main() {
var count = 5.5
print(count)
}
Output
5.5
Explicitly define
In this case, users have to define the data type.
fun main() {
var count:Double = 5.5
print(count)
}
Output
5.5
Boolean(true or false)
It is used to store either true or false values. When we want to perform conditional statements. On such conditions. The boolean data type is very useful. It is used to test the conditions.
Examples
Implicitly define
In this case, it automatically detects the data type is Boolean.
fun main() {
var access = true
print(access)
}
Output
true
Explicitly define
In this case, users have to define the data type.
fun main() { var access:Boolean = false print(access)
}
Output
false
Char(characters)
It is used to store the alphabet or characters. Either single and double quotes are used to define char.
Examples
Implicitly define
In this case, it automatically detects the data type is char.
Double Quotes Example
fun main() {
var str = "R"
print(str)
}
Output
R
Single Quotes Example
fun main() {
var str = 'R'
print(str)
}
Output
R
Explicitly define In this case, users have to define the data type. In case explicit only a single quote is used.
fun main() {
var str:Char = 'R'
print(str)
}
Output
R
String
It is used to store the sequence of characters. The string is enclosed between double-quotes.
Examples
Implicitly define
In this case, it automatically detects the data type is String.
fun main() {
var str = "Apkfear"
print(str)
}
Output
Apkfear
Explicitly define
In this case, users have to define the data type.
fun main() {
var str:String = "Apkfear"
print(str)
}
Output
Apkfear
Float
It is used to store the decimal value. It is suffixed with the letter 'F'.
Examples
Implicitly define
In this case, it automatically detects the data type is Float.
fun main() {
var count = 6.7F
print(count)
}
Output
6.7
Explicitly define
In this case, users have to define the data type.
fun main() {
var count:Float = 6.7F
print(count)
}
Output
6.7
Read also Kotlin operators.
Post a Comment
Post a Comment