![Kotlin-logo.png]()
Variables in Kotlin
What are Variables?
Variables in the programming world are addresses in memory that are reserved to store values
To define variables we use one of the key words (var OR val)
Then the name of the variable then the (:) tag then type of the variable and then assign its value using =
The most important types of variables:
Int: numerical variable
String: Variable Script
Bool: A logical variable that takes either the true value or the false value
Char: Variable Craftsman
Double, float: The decimal variable the difference between is storage capacity
EX:
var name:String = "mark"
var age:Int = 30
val distance:double = 3.9
val pi:double = 3.14
The difference between var and val is that the variable defined as val remains constant in the program and cannot be changed
while var can change the value of the variable later
Q1: What is the result of the following code:
fun main(args:Array <String>)
{
var name:String = "your name"
var age:Int = 66
println(name)
print(age)
}
Q2: What's wrong ?
fun main(args:Array <String>)
{
val pi:Double = 3.14
println(pi) Pi=5.7
print(pi)
}
Q3: What is the result of the following code:
fun main (arg:Array <String>)
{
var number:Int = 5
println(number)
number = 8
println(number)
}
Congratulations @marksadow! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of posts published
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP