Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 134 ยท Getting Started

Basic Types

A complete tour of Kotlin's basic types - Int, Long, Short, Byte, Double, Float, Boolean and Char - with literals, ranges, underscores and why there is no implicit widening.

Every value in a Kotlin program has a type, and the type decides what you can do with the value: you can add two numbers, but you cannot add a number to a yes/no answer. Kotlin has a small set of built-in types for numbers, truth values and characters, and they behave consistently. Unlike Java, Kotlin has no separate "primitive" types that you must learn alongside "wrapper" types; you write Int and the compiler chooses the most efficient representation.

This lesson introduces the whole family, shows how to write literals (the values you type directly into code), explains the limits of each numeric type, and explains the rule that surprises most newcomers: Kotlin never converts one number type into a wider one automatically.

The overview

Type Kind Size Range or meaning
Byte integer 8 bits -128 to 127
Short integer 16 bits -32,768 to 32,767
Int integer 32 bits about -2.1 billion to 2.1 billion
Long integer 64 bits about -9.2 quintillion to 9.2 quintillion
Float floating point 32 bits about 6-7 significant digits
Double floating point 64 bits about 15-16 significant digits
Boolean logical - true or false
Char character 16 bits one UTF-16 code unit, like 'A'
String text - a sequence of characters (next lessons)

Int and Double are the everyday defaults. You use the others when you have a reason: Long for big counts and timestamps, Byte for raw data, Float for large arrays of graphics numbers.

The numeric types are signed: they hold negative and positive values. Kotlin also has unsigned types (UInt, ULong, ...); they appear in Working with Numbers.

Integers

Whole numbers come in four sizes. The compiler picks a type for a literal: a whole number that fits in an Int is an Int, a larger one is a Long.

Kotlin Integer types and their limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Notice 3_000_000_000. You may put underscores inside a numeric literal to make it readable; they are ignored by the compiler. Use them for large numbers, binary values and phone-number-like constants.

Literal forms

Kotlin Writing numbers in different notations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Kotlin has no octal literals. Always use the capital L for Long (a lowercase l looks like the digit 1, and the compiler rejects it with use 'L' instead of 'l'.). The f or F suffix makes a Float; without it, a decimal number is a Double.

Overflow

Each integer type has a maximum. Go past it and the value silently wraps around to the other end, like a car odometer. Kotlin does not warn you at run time, so choose a type large enough.

Kotlin Wrapping around at the maximum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Int.MAX_VALUE + 1 gives -2147483648. When a calculation could exceed about two billion, use Long, or functions such as Math.addExact that throw instead of wrapping.

Floating-point numbers

Double and Float store numbers with a fractional part, but only approximately. Computers store them in binary, and many decimal fractions (like 0.1) cannot be written exactly in binary.

Kotlin Doubles are approximations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The first output is 0.30000000000000004, which is why you should never compare floating-point numbers with == when they come out of calculations; compare with a tolerance (abs(a - b) < 0.000001) or use BigDecimal for money (see Working with Numbers). Dividing by zero in floating point does not throw an exception; it gives Infinity or NaN (and NaN is not equal to anything, not even itself).

Which one? Use Double by default. Float saves memory in huge arrays but has only about seven significant digits, which surprises people quickly.

Boolean

A Boolean has exactly two values, true and false. Comparisons produce them, and the logical operators && (and), || (or) and ! (not) combine them. They drive if and loops.

Kotlin Booleans from comparisons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Unlike JavaScript or C, Kotlin never treats numbers or text as truthy or falsy: if (1) and if ("yes") are compile errors (condition type mismatch: inferred type is 'Int' but 'Boolean' was expected.). Only a real Boolean can be a condition. The && and || operators are short-circuiting: in a && b, b is not evaluated when a is already false. That is useful for guards such as list.isNotEmpty() && list[0] > 3.

Char

A Char is a single character in single quotes: 'A', '7', '?', '\n', 'A' (a Unicode escape). It is not a number, although it has a numeric code that you can read.

Kotlin Working with Char

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

You can add an Int to a Char (you get a Char) and subtract two Chars (you get an Int), but you cannot treat a Char as a number directly: '7' + 1 is the character '8', not the number 8. Use digitToInt() to convert a digit character to its value. The older Char.toInt() conversion is deprecated; use code, and prefer the Char(code) constructor function over Int.toChar().

Be careful with the quote styles: single quotes make a Char, double quotes make a String. 'ab' is an error (too many characters in a character literal.) because a Char is exactly one character.

No implicit widening

In many languages int x = 5; long y = x; just works, because the narrower value is silently converted to the wider type. Kotlin refuses. A smaller type is not a subtype of a larger one, and the compiler will not convert for you.

Kotlin No implicit conversion (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The compiler stops with main.kt:3:19: error: initializer type mismatch: expected 'Long', actual 'Int'. The reason is reliability: silent conversions hide precision loss and make == unpredictable. (In Java, new Integer(1).equals(new Long(1)) is false, a classic trap.) Kotlin makes you say what you mean, with explicit conversion functions.

Kotlin Explicit conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Every number type has toByte(), toShort(), toInt(), toLong(), toFloat() and toDouble(). Converting to a narrower type may lose information: 3.99.toInt() is 3 (truncation toward zero), and tooBig.toInt() gives a wrong-looking value. Remember: a conversion is the programmer saying "I know what I am doing."

There is one place where the compiler does help. A whole-number literal adapts to the integer type expected: val b: Byte = 1 and val l: Long = 2 are fine. It does not turn into a floating-point number, though: val d: Double = 2 is an error, so write 2.0. Arithmetic between mixed types also works, because operators are overloaded: 1 + 2L is a Long and 1 + 2.0 is a Double.

Kotlin Mixed arithmetic promotes the result

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Integer division discards the remainder (7 / 2 is 3), a classic source of bugs when you expect 3.5; one operand must be a floating-point number to get a fractional result. The next lesson, Working with Numbers, goes deeper.

Types in the type system: Any and nullability

All of these types sit in one hierarchy. Any is the root: every non-null value is an Any. Each type also has a nullable version (Int?, Boolean?) that can hold null. You will see in Null Safety how the compiler keeps nullable values from causing crashes, and in Any, Unit and Nothing how the special types fit in.

Kotlin Asking a value for its type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

::class.simpleName shows the type name at run time. Notice that the Kotlin names (Int, Long, Double, Float, Boolean, Char, String) are what you get, even though the JVM stores them in different ways inside.

Performance note

On the JVM, a non-null Int is stored as a raw 32-bit primitive in local variables, fields and arrays (IntArray), which is as fast as it gets. When an Int is stored in a generic container (List<Int>) or made nullable (Int?), it must be boxed into an object, which costs memory. For almost every program that does not matter. When you process millions of numbers in a hot loop, prefer IntArray or LongArray over List<Int> (see Arrays).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the type of val x = 3_000_000_000?

    The value does not fit into an Int (maximum about 2.1 billion), so the compiler chooses Long automatically. The underscores are only for readability.
  2. 2. Why does val big: Long = someInt not compile?

    Kotlin requires an explicit conversion such as someInt.toLong(). Silent conversions hide precision problems.
  3. 3. What does Int.MAX_VALUE + 1 evaluate to?

    Integer arithmetic on the JVM wraps around on overflow without any error. Use Long or Math.addExact when values can get large.
  4. 4. What does println(7 / 2) print?

    Both operands are Int, so this is integer division and the remainder is discarded. Use 7 / 2.0 to get 3.5.
  5. 5. Which of these is a Char?

    Single quotes create a Char, double quotes create a String.

Summary

  • The integer types are Byte, Short, Int and Long; Double and Float are the floating-point types; Boolean and Char complete the basics.
  • Int and Double are the defaults; a literal that is too big for Int becomes Long. Use L, f, hex (0xFF), binary (0b101) and underscores in literals.
  • Integer overflow wraps silently; floating-point numbers are approximate, and division by zero gives Infinity or NaN.
  • Kotlin never widens numbers implicitly. Convert with toLong(), toDouble() and friends; Char uses code and Char(code).
  • Int / Int is integer division. Only a real Boolean can be a condition.

Next: Working with Numbers