Kotlin Data Types — Int, String, Boolean and More
Kotlin is statically typed. Every variable has a type — either inferred or explicit. This tutorial covers all the common Kotlin data types with examples.
What You Will Learn
- All numeric types in Kotlin
- The Boolean and Char types
- How String works in Kotlin
- The Any, Unit, and Nothing types
- Type conversion between numbers
Overview of Kotlin Data Types
Common Data Types in Use
fun main() {
val age: Int = 25
val price: Double = 99.99
val isActive: Boolean = true
val grade: Char = 'A'
val website: String = "KotlinGuide"
println(age)
println(price)
println(isActive)
println(grade)
println(website)
}Each variable uses an explicit type. In practice, Kotlin infers most types, but writing them explicitly here makes the types easy to see.
Number Types
Working with Numbers
fun main() {
val count: Int = 1000
val distance: Long = 9_000_000_000L
val pi: Double = 3.14159
val tax: Float = 0.18f
println(count)
println(distance)
println(pi)
println(tax)
}Underscores in numbers (9_000_000_000) are ignored by the compiler — they just improve readability. L suffix marks a Long literal. f marks a Float.
Boolean Type
Boolean Variables
fun main() {
val isLoggedIn = true
val hasPermission = false
println(isLoggedIn)
println(isLoggedIn && hasPermission)
println(isLoggedIn || hasPermission)
}&& is logical AND (both must be true). || is logical OR (at least one must be true). This is the same as most other languages.
Practice Exercise
Which Kotlin type would you use to store the value 3.14?
Quick Quiz
What is the default type Kotlin infers for the literal 42?
Frequently Asked Questions
Related Tutorials
Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review