Using the 'lazy' Delegate for Lazy Initialization

The 'lazy' delegate in Kotlin allows for lazy initialization of properties, meaning that the property is only computed when it is accessed for the first time. This can improve performance by avoiding unnecessary computations and can also help in managing resources efficiently. The 'lazy' delegate is thread-safe by default, making it a great choice for properties that may be accessed from multiple threads.

val lazyValue: String by lazy {
    println("Computed!")
    "Hello, World!"
}

fun main() {
    println(lazyValue) // Computed! Hello, World!
    println(lazyValue) // Hello, World! (computed value is reused)
}