Values in Kotlin

Our first example will demonstrate various value types in Kotlin, including strings, integers, floats, and booleans. Here’s the full source code:

fun main() {
    // Strings, which can be concatenated with +
    println("kotlin" + "lang")

    // Integers and floats
    println("1+1 = ${1 + 1}")
    println("7.0/3.0 = ${7.0 / 3.0}")

    // Booleans, with boolean operators as you'd expect
    println(true && false)
    println(true || false)
    println(!true)
}

To run the program, save it as Values.kt and use the Kotlin compiler:

$ kotlinc Values.kt -include-runtime -d Values.jar
$ java -jar Values.jar
kotlinlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In this example, we’ve demonstrated:

  1. String concatenation using the + operator.
  2. Basic arithmetic operations with integers and floating-point numbers.
  3. Boolean operations including AND (&&), OR (||), and NOT (!).

Kotlin provides type inference, so we don’t need to explicitly declare variable types in these simple examples. The println function is used for output, similar to other languages.

Note that Kotlin uses string templates (like ${1 + 1}), which allow us to embed expressions directly in string literals. This is a convenient feature for formatting output.