Kotlin Variables Example
Learn to create and use variable to store values in Kotlin program. In this tutorial you will learn to declare basic variable types in this programming language and use in the programming.
Kotlin is statically typed language which means that it the type of variable must be declared at the time of compilation. In Kotlin you can define the variable type or leave it without any type. If you leave it without any type declaration then Kotlin will automatically assign a type to it based on the first value stored in the variable.
Kotlin compiler checks the variable type at the compilation time and generates error if it is not able to assign a type to it. Let's learn to define variables in Kotlin.
Here are two ways of declare variable in Kotlin:
var country = "French" val year = 2017
In the above example we are declaring variables using two type of keywords:
var - its variable and its value can be change after first initialization
val - its value can't be changed after first initialization. This is like static final in Java
Declaring String variables
You can define String variable in Kotlin in following ways:
//String variable declaration var name = "Deepak Kumar" //String type variable declaration var address: String address = "Delhi, India"
In the above example we have declared name variable without any type but assigned String value to it. Kotlin will automatically find out this and make name as String variable.
In the second line with declared address variable with type String. So, these are two ways to declare String variables in Kotlin.
In Kotlin you can't define a variable without type annotation or initialization. For example see following declaration:
var place place="Delhi"
Above declaration is not valid and compiler will throw error "This variable must either have a type annotation or be initialized".
Kotlin Basic Type
Just like Java Kotlin is strongly type programming language and its is required to provide type of variable at compile time. In Kotin data types can be grouped into:
- Numbers
- Characters
- Booleans
- Arrays
Number types in Kotlin
In Kotlin Number types are similar to Java numbers and these are:
- Byte
- Short
- Int
- Long
- Float
- Double
Byte Types
Following is example of declaration of Byte type variables:
//Byte Types var var1: Byte = 100 println(var1)
Short Types
Example of declaring short type variables.
//Short Types var var2: Short = 100 println(var2)
Int Types
Example of Int Type declaration in Kotlin:
//Int type var var3: Int = 100 println(var3)
Long Types
Here is example of declaration of Long type variables in Kotlin:
//Long Types var var4: Long = 100 println(var4)
Float Types
Here is example of defining Float variables in Kotlin programming language:
//Float Types var var5: Float = 10.0f println(var5)
Double Types
Example of creating Double type variable in Kotlin:
//Double Types var var6: Double = 12.0 println(var6)
Declaring Char variables
In the following we are declaring a Char type variable:
val letter: Char letter = 'A' println("$letter")
Boolean variables in Kotlin
In programming language there are two values of boolean, true or false. In Kotlin concept is the same and it can defined in following ways:
//Boolean variable var flag = true println("$flag")
Arrays in Kotlin
Like Java it also supports arrays of different types. Here is an example of declaration of in type array:
//Int arrays val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
Here is complete example of declaring different types of variables in Kotlin:
package net.roseindia import java.util.* fun main(args: Array) { println("Hello, World!") //Variable var country = "French" //Non mutable variable - Value can't be changed val year = 2017 //String variable declaration var name = "Deepak Kumar" //String type variable declaration var address: String address = "Delhi, India" var place="Delhi" //Byte Type var var1: Byte = 100 println(var1) //Short Type var var2: Short = 100 println(var2) //Int type var var3: Int = 100 println(var3) //Long Types var var4: Long = 100 println(var4) //Float Types var var5: Float = 10.0f println(var5) //Double Types var var6: Double = 12.0 println(var6) //Char variable var letter: Char letter = 'A' println("$letter") //Boolean variable var flag = true println("$flag") //Int arrays val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5) }
You can run this program in Eclipse and see the output.
In this tutorial you learned to declare and use basic data types in Kotlin