Avatar ·

What is the difference between init block and constructor in Kotlin?

Hello! I just started learning Kotlin after Java and encountered some confusion. In Java we have constructors where we initialize fields. In Kotlin I see that you can use both constructors (primary and secondary) and special `init` blocks. But I don't fully understand why `init` blocks are needed if everything can be done in the constructor? For example, I have this code: ```kotlin class Person(name: String) { val name: String init { this.name = name.uppercase() } } ``` Why not just write `init` in the constructor body, like in Java? Or here's another example: ```kotlin class Rectangle(val width: Int, val height: Int) { val area: Int init { area = width * height } } ``` Can this be done directly in the primary constructor? What's the