← Back to Kotlin Course | Chapter 2: Variables & Types | Lesson 3 of 7

Basic Types

Basic types are the different kinds of simple values Kotlin understands, like whole numbers, decimals, true/false, and single letters.

Integer Types

Int is the default type for whole numbers and holds values up to about 2.1 billion. Long holds much larger whole numbers but literals must end with an L suffix, such as 10000000000L.

Example: Integer Types

markup
fun main() {
    val small: Int = 42
    val big: Long = 10000000000L
    println("Int: $small, Long: $big")
}

Floating-Point Types

Double is the default type for decimal numbers with high precision, while Float uses less memory but less precision and requires an f suffix on its literals.

Example: Floating-Point Types

markup
fun main() {
    val pi: Double = 3.14159
    val ratio: Float = 1.5f
    println("Double: $pi, Float: $ratio")
}

Boolean and Char

Boolean can only be true or false, commonly used in conditions. Char holds exactly one character wrapped in single quotes, distinct from a String, which uses double quotes.

Example: Boolean and Char

markup
fun main() {
    val isKotlinFun: Boolean = true
    val grade: Char = 'A'
    println("Fun: $isKotlinFun, Grade: $grade")
}

Integer Division Truncates

Dividing two Int values with / performs integer division, discarding any remainder rather than rounding. To get a fractional result, at least one operand must be a Double or Float.

Note: Use the % operator to get the remainder of an integer division.

Example: Integer Division Truncates

markup
fun main() {
    val a = 7
    val b = 2
    println("Integer division: ${a / b}")
    println("Double division: ${a / b.toDouble()}")
}
Common Mistakes
  1. Assuming Int and Long are interchangeable; large values need Long and a literal must be suffixed with L.
  2. Confusing Char (single character, single quotes) with String (text, double quotes).
  3. Forgetting that dividing two Int values performs integer division, truncating any decimal part.
Chapter Summary
  • Kotlin's core numeric types include Int, Long, Double, and Float, each with a different size and precision.
  • Boolean holds true or false, and Char holds a single character in single quotes.
  • String holds text in double quotes and is a distinct type from Char.
  • Integer division between two Int values truncates the decimal part; use Double operands to get a fractional result.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.