Type annotation

In Kotlin, we can also declare a specific type of variable with var variableName : nameOfDataType type annotation:

var myInteger : Int
var myString : String
val myDouble : Double

Once the variables are declared, we can assign values to them:

myInteger = 10
myString = "Hello"
myDouble = 12.123

Having variable declarations with explicit data types and assigning a value at the same time is also possible:

val myInt : Int = 10
var myString : String = "Hello"

Variable declaration and initialization is explained in the following example:

fun main(args: Array<String>) {
var myName: String // Variables can be initialized explicitly
myName = "Jon" // Initialization
// declaration and initialization in one line
var myInt: Int = 10
var myLong: Long = 11
var myShort: Short = 11
var myByte: Byte = -128
var d1 = 5.10 // Declaration of Double and Float
var d2: Double = 5.10
var f1 = 5.10
var f2: Float = 5.10f
}

Kotlin data types are divided into the following groups:

  • Number data types 
  • Real data types
  • Boolean data types 
  • Character data types

Now let's have a look at each category in the following sections. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.240.252