Variables in kotlin
package com.example.my_kotlin_practice
fun main(args: Array<String>)
{
var name = "Helle World"
var age = 22
var height = 8
println(name)
println(age)
println(height)
// variable values using $ sign
println("Name : $name")
println("Age : $age")
println("Height : $height")
// variable values without using $ sign
println("Name : " + name)
println("Age : " + age)
println("Height : " + height)
// Mutable variables
name = "How are you ?"
age = 24
println("Name : $name")
println("Age : $age")
// Read-only variables
/* A read-only variable can be declared using val (instead of var) and once a value is assigned,
* it can not be re-assigned */
val bat : String = "Tomorrow"
val toy : Int = 20
println("Bat : $bat")
println("Toy : $toy")
//bat = "Hello brother" //Not allowed, throws an exception
//toy = 22 //Not allowed, throws an exception
//Variables types
var name2 : String = "Today"
var age2 : Int = 45
println("Name2 : $name2")
println("Age2 : $age2")
// Kotlin variable names can contain letters, digits, underscores, and dollar signs.
// Kotlin variable names should start with a letter or underscores
// 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.
}
OUTPUT :
Helle World 22 8 Name : Helle World Age : 22 Height : 8 Name : Helle World Age : 22 Height : 8 Name : How are you ? Age : 24 Bat : Tomorrow Toy : 20 Name2 : Today Age2 : 45 Process finished with exit code 0
Comments
Post a Comment