Welcome back to part 4 of Kotlin tutorials, In this post, we’ll discuss about variables in Kotlin.
Basically, we use two types of keywords for declaring variables in Kotlin:
- var
- val
var– You can re-assign a value.
val- You can’t re-assign a value, it can be assigned only once.
You can imagine variables as containers that hold data value.
Suppose, we’ve some values and we want to use that value in multiple places and we want to change that value from a single place, for that situation we can use variables.
So, this is a short overview of variables, below we’ll see more details about variables with examples.
Page Contents
Let’s see, how we can use variables in the Kotlin.
- Go to IntelliJ idea or Android Studio.
- Create a simple class.
- Write the main function.
Example 1-
fun main() { print("Techpass Master") }
In the above program, you will see, we are directly printing Techpass Master(“Techpass Master”), now suppose if we want to define a string so what is the way of defining the variable, for that in this main function we have to use the keyword of var or val like var myWebsite = “Techpass Master”
fun main() { var myWebsite = "Techpass Master" print(myWebsite) // in the print stetment simply pass (myWebsite) }
Now run the program and you’ll get the output:
Techpass Master
Example 2-
Write the main function.
fun main() { print("Hello Kotlin, Our website name is: Techpass Master") }
Run the program and you’ll get the output:
Hello Kotlin, Our Website name is: Techpass Master
In the above program, you will see in the output nothing is special and our program looking like inflexible because we are directly printing the website name (Techpass Master) in the print statement. Now we will pass the website name dynamically with the help of variables.
fun main() { var myWebsiteName = "Techpass Master" print( myWebsiteName) // in the print statement simply pass (myWebsiteName) }
Now run the program and you will get the output:
Techpass Master
Suppose, if you want to use the variable in multiple places and we want to change it from a single place for that, we can use variable var or val.
fun main() { var myDomain = "Xyz" println(myDomain) println(myDomain) println(myDomain) println(myDomain) }
Run the program and you will get the output:
Xyz Xyz Xyz Xyz
In the above program you can see, we are printing the variable value multiple times and we can also change the website name “Xyz” from a single place.
Same, you will define other data types like String, Int, Float, Double, etc:
fun main() { var myString = "Hello Word" // String Data Type var myInt = 10 // Int Data Type var myFloat = 20.0 // Float Data Type var myDouble = 50.00000 // Double Data Type println(myString) // print string value println(myInt) // print int value println(myFloat) // print float value println(myDouble) // print double value }
Run the program and you will get the output like this:
Hello Word 10 20.0 50.0
Now let see how we can Declare:
- static variable in Kotlin.
- global variable in Kotlin.
- boolean variable in Kotlin.
- integer variable in Kotlin.
- float variable in Kotlin.
- double variable in Kotlin.
- string variable in Kotlin.
Let’s see variable declaration one by one with examples:
Declare static variable in Kotlin
In Kotlin you can’t create static variable and methods, if you want to use static variable and methods, so you have to declare an object or companion object inside your class, below you can see the example:
fun main(){ val getStaticVariable = StaticVariable.staticVariable print(getStaticVariable) } class StaticVariable { companion object{ val staticVariable ="Hello Techpass Master" } }
Declare global variable in Kotlin.
First, we have to need to understand the difference between local variable and global variables.
- Local Variable – Local variable is a variable that is declared in the particular block, where it is declared.
- Global Variable – Global variable is a variable that is declared in all blocks and functions and it is declared at the top of the program and outside all function and block. below see example:
fun main() { val globalAndLocalVariable = GlobalAndLocalVariable() globalAndLocalVariable.childFunction1() globalAndLocalVariable.childFunction2() } class GlobalAndLocalVariable { val globalVariableName = "This is a Global Variable" // this is global variable fun childFunction1() { val localVariableName1 = "This is a Local Variable (childFunction1)" println(globalVariableName) } fun childFunction2() { val localVariableName2 = "This is a Local Variable (childFunction2)" println(globalVariableName) // here you can see we are able to call global variable // print(localVariableName1) // but when we will try to call localVariableName1 from childFunction2 then we got an exception (Unresolved reference: localVariableName1) } }
Declare boolean variable in Kotlin.
Boolean keyword-only takes the values true or false:
fun main() { val isSwitchOn: Boolean = true val isSwitchOff: Boolean = false println(isSwitchOn) // Output true println(isSwitchOff) // Output false }
The above example you can also be written without specifying the type.
fun main() { val isSwitchOn = true // without specifying the type. val isSwitchOff= false // without specifying the type. println(isSwitchOn) // Output true println(isSwitchOff) // Output false }
Declare integer variable in Kotlin.
fun main() { var myInt = 22 // Interger Data Type println(myInt) // Output 22 }
Declare float variable in Kotlin.
fun main() { var myFloat = 40.0 // Float Data Type println(myFloat) // Output 40.0 }
Declare double variable in Kotlin.
fun main() { var myDouble = 1000000.0 // Double Data Type println(myDouble) // Output 1000000.0 }
Declare string variable in Kotlin.
fun main() { var myString = "Techpass Master" // String Data Type println(myString) // Output Techpass Master }
Read More ⇣