tutorial, no_image, kotlin,

Code - no_image

Upendra Upendra Follow Jan 23, 2025 · 1 min read
Code - no_image
Share this

How to create empty constructor for data class?

Click to expand! If you give default value to all the fields - empty constructor is generated automatically by Kotlin: ``` data class Information( var title: String = "", var text: String = "", ) ``` or you can declare a secondary constructor that nas no parameter: ``` data class Information( var title: String, var text: String, ) { constructor(): this("", "") } ```

What will be result of the following code execurion?

val innerProperty by lazy { 
    println("Calculating the value")
    "Result"
}

fun main(args: Array<String>) {
  println(innerProperty)
  println(innerProperty)
}
Click to expand! For lazy the first time you access the Lazy property, the initialisation (lazy() function invocation) takes place. The second time, this value is remembered and returned: ``` Calculating the value Result Result ```
credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.