Constants in Kotlin

Go supports constants of character, string, boolean, and numeric values.

package main

import kotlin.math.sin

const val s: String = "constant"

fun main() {
    println(s)

    const val n = 500000000
    println(n)

    // Constant expressions perform arithmetic with arbitrary precision.
    const val d = 3e20 / n
    println(d)

    // A numeric constant has no type until it’s given one, such as by an explicit conversion.
    println(d.toLong())

    // A number can be given a type by using it in a context that requires one, such as a variable
    // assignment or function call. For example, here `sin` expects a `Double`.
    println(sin(n.toDouble()))
}

To run the program, put the code in Main.kt and use the Kotlin compiler to execute it.

$ kotlinc Main.kt -include-runtime -d Main.jar
$ java -jar Main.jar
constant
6.0E11
600000000000
-0.28470407323754404

Now that we can run and build basic Kotlin programs, let’s learn more about the language.