Basic Types
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
fun main() {
val small: Int = 42
val big: Long = 10000000000L
println("Int: $small, Long: $big")
}
Login to try C/C++/Java/PHP code in the editor
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
fun main() {
val pi: Double = 3.14159
val ratio: Float = 1.5f
println("Double: $pi, Float: $ratio")
}
Login to try C/C++/Java/PHP code in the editor
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
fun main() {
val isKotlinFun: Boolean = true
val grade: Char = 'A'
println("Fun: $isKotlinFun, Grade: $grade")
}
Login to try C/C++/Java/PHP code in the editor
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
fun main() {
val a = 7
val b = 2
println("Integer division: ${a / b}")
println("Double division: ${a / b.toDouble()}")
}
Login to try C/C++/Java/PHP code in the editor
- Assuming
IntandLongare interchangeable; large values needLongand a literal must be suffixed withL. - Confusing
Char(single character, single quotes) withString(text, double quotes). - Forgetting that dividing two
Intvalues performs integer division, truncating any decimal part.
- Kotlin's core numeric types include
Int,Long,Double, andFloat, each with a different size and precision. Booleanholdstrueorfalse, andCharholds a single character in single quotes.Stringholds text in double quotes and is a distinct type fromChar.- Integer division between two
Intvalues truncates the decimal part; useDoubleoperands to get a fractional result.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: