
This is a chapter from the book Kotlin Essentials. You can find it on LeanPub or Amazon. It is also available as a course.
fun main() { println("Hello, World") }
console in JavaScript), and no conditions (like in Python when we start code in the IDE). We need the main function and the println function call with some text[^02_0].Array<String>:fun main(args: Array<String>) { println("Hello, World") }
main function:fun main(vararg args: String) { println("Hello, World") }
class Test { companion object { @JvmStatic fun main(args: Array<String>) { println("Hello, World") } } }
suspend fun main() { println("Hello, World") }
main function as we will find it most useful. I will use it in nearly every example in this book. Such examples are usually completely executable if you just copy-paste them into IntelliJ or the Online Kotlin Playground[^02_2].fun main() { println("Hello, World") }
main function in IntelliJ is click the green triangle which appears on the left side of the main function; this is called the "gutter icon", also known as the "Run" button.
main function quite often. Here come live templates to help us. This is an IntelliJ feature that suggests using a template when you start typing its name in a valid context. So, if you start typing "main" or "maina" (for main with arguments) in a Kotlin file, you will be shown a suggestion that offers the whole main function.
main function with the live template “maina”. Use the print function with some text, and run the code with the Run button.println("Hello, World") inside.fun main(args: Array<String>) { println("Hello, World") }



main function on JVM becomes a static function inside a class named PlaygroundKt. Where does this name come from? Try to guess. Yes, this is, by default, the file's name with the "Kt" suffix. The same happens to all other functions and properties defined outside of classes on JVM.main function from Java code, we can call PlaygroundKt.main({}).PlaygroundKt can be changed by adding the @file:JvmName("NewName") annotation at the top of the file[^02_6]. However, this does not change how elements defined in this file are used in Kotlin. It only influences how we will use such functions from Java. For example, to call our main function from Java now, we would need to call NewName.main({}).- How Kotlin code works on a low level.
- How a certain Kotlin feature works under the hood.
- How to use a Kotlin element in Java.
package keyword.package com.marcinmoskala.domain.model class User(val name: String)
com.marcinmoskala. We name package using lowercase characters only.* character to import all elements from a package.package com.marcinmoskala.domain import com.marcinmoskala.domain.model.User // or import com.marcinmoskala.domain.model.* fun useUser() { val user = User("Marcin") // ... }
println function without importing it.main functions and creating them easily with live templates. We’ve also learned how to find out what our Kotlin code would look like if it were written in Java. For me, it seems like we have quite a nice toolbox for starting our adventure. So, without further ado, let's get on with that.println function is implicitly imported from the standard library package kotlin.io.[^02_2]: You can also find some chapters of this book online on the Kt. Academy website. These examples can be started and modified thanks to the Kotlin Playground feature.
[^02_3]: It makes me happy when people try to challenge what I am teaching. Be skeptical, and verify what you've learned; this is a great way to learn something new and deepen your understanding.
[^02_5]: This doesn’t always work because the decompiler is not perfect, but it is really helpful anyway.
[^02_6]: More about this in the book Advanced Kotlin, chapter Kotlin and Java interoperability.
[^02_7]: By elements in the context of Kotlin programming we mean classes, functions, properties, object, interfaces, etc. We will discuss all element types in the following chapters.
