Datatypes

Datatype rules for Kotlin.

fun main() {
    val myInt = 10
    println("default datatype is ${myInt is Int}")
    var myLong = 22L
    // Every datatype has .toXxxx() functions to convert to another type. In Kotlin, you cannot declare myLong = myInt and expect it to expand the data type.
    myLong = myInt.toLong()

    val myByte: Byte = 111
    var myShort: Short
    myShort = myByte.toShort()

    var myDouble = 65.984 // Defaults to a Double
    println(myDouble is Double) // As proof

    val myFloat = 838.9823f // f declares it a Float
    println(myFloat is Float) // As proof

//    myDouble = myFloat // Will fail - no expansion
    myDouble = myFloat.toDouble()

    val char = 'b' // Defaults to Char
//    char myChar = 65 // In Java would call the char of value 65 = 'A'
    val myChar = 65 // In Kotlin, obviously produces a very different result!
//    val myChar2: Char = 65 // Fails, does not conform to data type
    val myCharInt = 65
    println(myCharInt.toChar()) // => A

    // When calling Java from Kotlin, how do we provide primitive types since they don't exist in Kotlin?
    val vacationTime = false
    val onVacation = DummyClass().isVacationTime(vacationTime)
    println(onVacation) // Works because the Kotlin datatype classes compile down to primitives. Also works if Java is expecting Boolean due to Java boxing.

    // Any, Unit, Nothing classes
    // Any is Kotlin root class
    val anything: Any
    // Unit class is equivalent to void. A Singleton instance of Unit is returned instead of nothing
    // Nothing class is subclass of Any class. Could use when e.g. you know something will never return
}