Kotlin Variables & Data Types
Learn about variables, constants, and data types in Kotlin.
Variable Declaration
// Mutable variable (can be reassigned)
var count: Int = 10
count = 15 // Valid
// Immutable variable (cannot be reassigned)
val name: String = "Kotlin"
name = "Java" // Compilation error
// Type inference
val message = "Hello" // Inferred as String
val pi = 3.14 // Inferred as Double
Basic Data Types
| Type | Description | Example |
|---|---|---|
| Int | 32-bit integer | 42, -7 |
| Long | 64-bit integer | 42L, -7L |
| Double | 64-bit floating point | 3.14, -2.71 |
| Float | 32-bit floating point | 3.14f, -2.71f |
| Boolean | True or false | true, false |
| Char | Single character | 'A', '\n' |
| String | Sequence of characters | "Hello", "Kotlin" |
Type Conversion
val number: Int = 42
val longNumber: Long = number.toLong()
val stringNum = "123"
val intNum = stringNum.toInt()
String Templates
val name = "Alice"
println("Hello, $name!") // Hello, Alice!
val a = 10
val b = 20
println("Sum: ${a + b}") // Sum: 30
← Back to Tutorials