Kotlin Data Types
Kotlin includes several built-in data types that you will use frequently as you write programs. Understanding these types helps you store and work with different kinds of information in your code. The most common data types in Kotlin are:
- Int: use this type to store whole numbers, such as counts, ages, or any value without a fractional part;
- Double: use this type for numbers with decimals, like measurements or calculations that require precision;
- Boolean: use this type for values that can only be either
trueorfalse, such as flags or conditions; - String: use this type to store text, such as names, messages, or any sequence of characters.
Each data type serves a specific purpose, and choosing the right one makes your code clearer and safer. Next, you will see how to declare variables of these types and print their values.
Main.kt
1234567891011121314151617package com.example fun main() { // Integer value val age: Int = 25 // Double value val height: Double = 1.75 // Boolean value val isStudent: Boolean = true // String value val name: String = "Alex" println("Name: $name") println("Age: $age") println("Height: $height") println("Is student: $isStudent") }
In this code, you see four variables, each using a different Kotlin data type. The variable age is declared as an Int to hold a whole number. The variable height uses the Double type for a decimal value. The isStudent variable is a Boolean, representing a true/false condition. Finally, name is a String, storing a sequence of characters. Each variable is printed using string templates, which let you easily include variable values in text output. This example shows how each data type is used in practice and how you can display their values in your program.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Fantastisk!
Completion rate forbedret til 6.25
Kotlin Data Types
Sveip for å vise menyen
Kotlin includes several built-in data types that you will use frequently as you write programs. Understanding these types helps you store and work with different kinds of information in your code. The most common data types in Kotlin are:
- Int: use this type to store whole numbers, such as counts, ages, or any value without a fractional part;
- Double: use this type for numbers with decimals, like measurements or calculations that require precision;
- Boolean: use this type for values that can only be either
trueorfalse, such as flags or conditions; - String: use this type to store text, such as names, messages, or any sequence of characters.
Each data type serves a specific purpose, and choosing the right one makes your code clearer and safer. Next, you will see how to declare variables of these types and print their values.
Main.kt
1234567891011121314151617package com.example fun main() { // Integer value val age: Int = 25 // Double value val height: Double = 1.75 // Boolean value val isStudent: Boolean = true // String value val name: String = "Alex" println("Name: $name") println("Age: $age") println("Height: $height") println("Is student: $isStudent") }
In this code, you see four variables, each using a different Kotlin data type. The variable age is declared as an Int to hold a whole number. The variable height uses the Double type for a decimal value. The isStudent variable is a Boolean, representing a true/false condition. Finally, name is a String, storing a sequence of characters. Each variable is printed using string templates, which let you easily include variable values in text output. This example shows how each data type is used in practice and how you can display their values in your program.
Takk for tilbakemeldingene dine!