Let's look at how Kotlin code is structured. I promise it's refreshingly simple compared to some other languages.
The fun main() function
In Kotlin, fun is how you declare a function. The main() function is the entry point โ the first thing that runs when you execute your program.
fun main() {
println("Welcome to Kotlin")
}
Nothing else needed. No class wrapper, no public static void. Just fun main() and curly braces.
No semicolons
You don't need semicolons at the end of each line. In fact, don't use them. Kotlin handles line endings automatically. One less thing to worry about.
fun main() {
println("No semicolons")
println("See? Clean.")
}
Packages and imports
Just like Java, you can organize your code in packages. The import keyword brings in code from other packages.
package myapp
import kotlin.math.sqrt
fun main() {
println(sqrt(16.0))
}
The package declaration goes at the top, then imports, then your code. But for simple programs, you don't even need the package line.
Try it Yourself โ