Basic Types
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
// 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")
}
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
// 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")
}
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
// 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")
}
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.
% operator to get the remainder of an integer division.Example: Integer Division Truncates
// 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()}")
}
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: