← 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.
Syntax
kotlin
val name: Int = 42
val name: Long = 10000000000L
val name: Double = 3.14
val name: Float = 1.5f
val name: Boolean = true
val name: Char = 'c'

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
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `small` (an Int), set to `42`
    val small: Int = 42
    // Declare a read-only variable `big` (a Long), set to `10000000000L`
    val big: Long = 10000000000L
    // Print "Int: $small, Long: $big" to the console, with a trailing newline
    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
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `pi` (a Double), set to `3.14159`
    val pi: Double = 3.14159
    // Declare a read-only variable `ratio` (a Float), set to `1.5f`
    val ratio: Float = 1.5f
    // Print "Double: $pi, Float: $ratio" to the console, with a trailing newline
    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
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `isKotlinFun` (a `Boolean`), set to `true`
    val isKotlinFun: Boolean = true
    // Declare a read-only variable `grade` (a `Char`), set to `'A'`
    val grade: Char = 'A'
    // Print "Fun: $isKotlinFun, Grade: $grade" to the console, with a trailing newline
    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
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `a`, set to `7`
    val a = 7
    // Declare a read-only variable `b`, set to `2`
    val b = 2
    // Print "Integer division: ${a / b}" to the console, with a trailing newline
    println("Integer division: ${a / b}")
    // Print "Double division: ${a / b.toDouble()}" to the console, with a trailing newline
    println("Double division: ${a / b.toDouble()}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
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.