Menu
Coddy logo textTech

Numbers

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 7 of 93.

Use an Int for whole numbers and a Double for fractional numeric values. Kotlin infers these types from the literal:

val seats = 6
val unitPrice = 2.75
println(seats)       // 6
println(unitPrice)   // 2.75

An Int has a limited range. Use Long for larger whole numbers, with an L suffix on the literal:

val distance: Long = 3000000000L
println(distance) // 3000000000

Numeric literals have no quotes. A decimal point gives a fractional numeric type, while quotes would create text. Choose a name that describes the number and use val when you do not need reassignment.

challenge icon

Challenge

Beginner

Write code that initializes a value named myVar with the value 5.

  • Replace the ? with the correct value
  • Lines starting with // are comments. They explain what to do but don't affect your code
  • Only change the line that says val myVar = ?

Try it yourself

fun main() {
    // Type your code below
    val myVar = ?

    // Don't change the line below
    println("myVar = $myVar")
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Kotlin playground