Kotlin variables are named storage locations in memory that hold data values during program execution. Unlike some programming languages, Kotlin variables offer both mutable and immutable options, giving developers flexibility in managing data state. When you declare Kotlin variables, you’re essentially creating placeholders that can store different types of information like numbers, text, or complex objects.
Mutable Kotlin variables are declared using the var
keyword and can be modified after initialization. These Kotlin variables allow you to change their values throughout the program execution.
var userName = "Alice"
userName = "Bob" // Valid - can be reassigned
Immutable Kotlin variables use the val
keyword and cannot be reassigned once initialized. These Kotlin variables promote safer coding practices by preventing accidental modifications.
val apiKey = "abc123xyz"
// apiKey = "newKey" // Compilation error - cannot reassign
Kotlin variables follow a specific syntax pattern that makes code readable and type-safe. The basic structure for declaring Kotlin variables includes the keyword, variable name, optional type annotation, and initialization value.
When declaring Kotlin variables with explicit types, you specify the data type after the variable name using a colon.
var studentAge: Int = 20
val courseName: String = "Kotlin Programming"
var isEnrolled: Boolean = true
Kotlin variables support type inference, automatically determining the variable type based on the assigned value. This feature makes Kotlin variables declaration more concise while maintaining type safety.
var currentScore = 85 // Inferred as Int
val welcomeMessage = "Hello, Kotlin!" // Inferred as String
var hasCompleted = false // Inferred as Boolean
Kotlin variables can store various numeric types, each with specific ranges and use cases.
Integer Types:
var byteValue: Byte = 127
var shortValue: Short = 32000
var intValue: Int = 1000000
var longValue: Long = 9223372036854775807L
Floating-Point Types:
var floatPrice: Float = 19.99f
var doublePrice: Double = 299.999
Text-based Kotlin variables handle individual characters and complete strings efficiently.
var grade: Char = 'A'
var fullName: String = "John Doe"
var multilineText: String = """
This is a multi-line
string in Kotlin
""".trimIndent()
Boolean Kotlin variables store true or false values, essential for conditional logic and program flow control.
var isLoggedIn: Boolean = false
val hasPermission = true
var canEdit = isLoggedIn && hasPermission
Kotlin variables can be nullable, allowing them to hold null values when explicitly declared with the nullable type syntax.
var optionalName: String? = null
var nullableAge: Int? = 25
optionalName = "Sarah" // Valid assignment
optionalName = null // Also valid for nullable variables
When working with nullable Kotlin variables, use safe call operators to prevent null pointer exceptions.
var userEmail: String? = "user@example.com"
val emailLength = userEmail?.length // Safe call returns Int? or null
Some Kotlin variables cannot be initialized immediately but need to be set before first use. The lateinit
modifier helps with such scenarios.
lateinit var databaseConnection: String
// Initialize later in the program
databaseConnection = "jdbc:mysql://localhost:3306/mydb"
For expensive computations or resource-intensive operations, Kotlin variables can use lazy initialization to defer value calculation until first access.
val expensiveData: String by lazy {
// This block executes only when accessed for the first time
"Computed result after heavy processing"
}
Local Kotlin variables exist within specific functions or code blocks and have limited scope.
fun calculateTotal() {
var localSum = 0 // Local variable
val taxRate = 0.08 // Local constant
localSum = 100 + (100 * taxRate).toInt()
}
Class-level Kotlin variables, known as properties, belong to class instances and can be accessed throughout the class.
class Student {
var studentId: Int = 0 // Mutable property
val universityName: String = "Tech University" // Immutable property
private var gpa: Double = 0.0 // Private property
}
Let’s explore comprehensive examples demonstrating various Kotlin variables concepts in real-world scenarios.
// File: StudentManager.kt
class StudentManager {
// Class-level Kotlin variables
private var totalStudents: Int = 0
private val maxCapacity: Int = 100
private var studentList: MutableList<String> = mutableListOf()
fun addStudent(name: String): Boolean {
// Local Kotlin variables
val canAddStudent = totalStudents < maxCapacity
var successMessage: String? = null
return if (canAddStudent) {
studentList.add(name)
totalStudents++
successMessage = "Student $name added successfully"
println(successMessage)
true
} else {
val errorMessage = "Cannot add student. Maximum capacity reached."
println(errorMessage)
false
}
}
fun getStudentInfo(): String {
val currentCount = totalStudents
val remainingSlots = maxCapacity - currentCount
return "Total Students: $currentCount, Remaining Slots: $remainingSlots"
}
}
fun main() {
// Creating instance and using Kotlin variables
val manager = StudentManager()
// Mutable Kotlin variables for user input simulation
var studentName1 = "Alice Johnson"
var studentName2 = "Bob Smith"
val studentName3 = "Carol Williams"
// Adding students using Kotlin variables
manager.addStudent(studentName1)
manager.addStudent(studentName2)
manager.addStudent(studentName3)
// Display information
println(manager.getStudentInfo())
// Demonstrating nullable Kotlin variables
var optionalStudent: String? = null
optionalStudent?.let { manager.addStudent(it) } // Safe call - won't execute
optionalStudent = "David Brown"
optionalStudent?.let { manager.addStudent(it) } // Will execute
println(manager.getStudentInfo())
}
// File: PriceCalculator.kt
class PriceCalculator {
// Immutable Kotlin variables for configuration
private val taxRate: Double = 0.12
private val shippingThreshold: Double = 50.0
private val shippingCost: Double = 8.99
// Mutable Kotlin variables for state
private var discountPercentage: Double = 0.0
private var customerType: String = "regular"
// Lazy initialization for expensive computation
private val premiumDiscounts by lazy {
mapOf(
"premium" to 0.15,
"gold" to 0.20,
"platinum" to 0.25
)
}
fun setCustomerType(type: String) {
customerType = type
// Update discount based on customer type
discountPercentage = when (type.lowercase()) {
"premium" -> premiumDiscounts["premium"] ?: 0.0
"gold" -> premiumDiscounts["gold"] ?: 0.0
"platinum" -> premiumDiscounts["platinum"] ?: 0.0
else -> 0.0
}
}
fun calculateTotalPrice(basePrice: Double): PriceBreakdown {
// Local Kotlin variables for calculations
val discountAmount = basePrice * discountPercentage
val discountedPrice = basePrice - discountAmount
val taxAmount = discountedPrice * taxRate
val needsShipping = discountedPrice < shippingThreshold
val finalShippingCost = if (needsShipping) shippingCost else 0.0
val totalPrice = discountedPrice + taxAmount + finalShippingCost
return PriceBreakdown(
basePrice = basePrice,
discountAmount = discountAmount,
taxAmount = taxAmount,
shippingCost = finalShippingCost,
totalPrice = totalPrice
)
}
}
// Data class to hold price breakdown
data class PriceBreakdown(
val basePrice: Double,
val discountAmount: Double,
val taxAmount: Double,
val shippingCost: Double,
val totalPrice: Double
)
fun main() {
val calculator = PriceCalculator()
// Different Kotlin variables for testing
var productPrice = 75.00
val customerTypes = listOf("regular", "premium", "gold", "platinum")
// Test with different customer types
for (type in customerTypes) {
calculator.setCustomerType(type)
val breakdown = calculator.calculateTotalPrice(productPrice)
println("=== $type Customer ===")
println("Base Price: $${String.format("%.2f", breakdown.basePrice)}")
println("Discount: -$${String.format("%.2f", breakdown.discountAmount)}")
println("Tax: $${String.format("%.2f", breakdown.taxAmount)}")
println("Shipping: $${String.format("%.2f", breakdown.shippingCost)}")
println("Total: $${String.format("%.2f", breakdown.totalPrice)}")
println()
}
// Testing with nullable Kotlin variables
var optionalPrice: Double? = null
optionalPrice = 25.0 // Below shipping threshold
calculator.setCustomerType("regular")
val lowPriceBreakdown = calculator.calculateTotalPrice(optionalPrice)
println("=== Low Price Item ===")
println("Total with shipping: $${String.format("%.2f", lowPriceBreakdown.totalPrice)}")
}
// File: ConfigurationManager.kt
object ConfigurationManager {
// Late-initialized Kotlin variables
lateinit var databaseUrl: String
lateinit var apiKey: String
// Regular Kotlin variables
private var isInitialized: Boolean = false
private val defaultTimeout: Long = 30000L
var connectionTimeout: Long = defaultTimeout
private set
// Nullable Kotlin variables for optional settings
var debugMode: Boolean? = null
var logLevel: String? = null
fun initialize(dbUrl: String, key: String) {
databaseUrl = dbUrl
apiKey = key
isInitialized = true
// Set default values for nullable variables if not provided
debugMode = debugMode ?: false
logLevel = logLevel ?: "INFO"
}
fun updateTimeout(timeout: Long) {
connectionTimeout = if (timeout > 0) timeout else defaultTimeout
}
fun getConfigSummary(): String {
return if (::databaseUrl.isInitialized && ::apiKey.isInitialized) {
"""
Configuration Status: Initialized
Database URL: ${databaseUrl.take(20)}...
API Key: ${apiKey.take(8)}...
Connection Timeout: ${connectionTimeout}ms
Debug Mode: ${debugMode}
Log Level: ${logLevel}
""".trimIndent()
} else {
"Configuration not initialized"
}
}
}
// Usage class demonstrating Kotlin variables
class ApplicationBootstrap {
private var appName: String = "MyKotlinApp"
private val version: String = "1.0.0"
private var startupTime: Long = 0L
fun startApplication() {
startupTime = System.currentTimeMillis()
// Initialize configuration with Kotlin variables
val dbConnection = "jdbc:postgresql://localhost:5432/myapp"
val secretKey = "sk_live_1234567890abcdef"
// Setting optional Kotlin variables
ConfigurationManager.debugMode = true
ConfigurationManager.logLevel = "DEBUG"
// Initialize the configuration
ConfigurationManager.initialize(dbConnection, secretKey)
ConfigurationManager.updateTimeout(45000L)
println("=== $appName v$version Started ===")
println("Startup Time: $startupTime")
println(ConfigurationManager.getConfigSummary())
}
}
fun main() {
// Main function Kotlin variables
val app = ApplicationBootstrap()
var attempts = 0
val maxAttempts = 3
// Simulate application startup with retry logic
while (attempts < maxAttempts) {
try {
attempts++
app.startApplication()
println("Application started successfully on attempt $attempts")
break
} catch (e: Exception) {
println("Startup attempt $attempts failed: ${e.message}")
if (attempts == maxAttempts) {
println("Failed to start application after $maxAttempts attempts")
}
}
}
}
Expected Output:
=== MyKotlinApp v1.0.0 Started ===
Startup Time: 1654812345678
Configuration Status: Initialized
Database URL: jdbc:postgresql://l...
API Key: sk_live_1...
Connection Timeout: 45000ms
Debug Mode: true
Log Level: DEBUG
Application started successfully on attempt 1
These comprehensive examples demonstrate how Kotlin variables work in practical scenarios, showing mutable and immutable variables, nullable types, late initialization, lazy properties, and proper scoping. The examples include all necessary imports and provide clear output expectations, helping you understand how Kotlin variables behave in real applications.