What is the entry point of a Kotlin program, and what are the valid signatures for `main`?
answer
- main = where the program starts
- Two forms: main() or main(args: Array<String>)
- Top-level, no class required
- args excludes the program name
- Compiles to AppKt with synthetic static main
basics
~10 sEvery Kotlin program starts in a function called main. You can write fun main() with no parameters, or fun main(args: Array<String>) to receive command-line arguments. The runtime calls it automatically to begin execution.
solid answer
~40 sKotlin's entry point is a top-level function named `main`. Two valid forms exist: `fun main()` (no parameters, introduced in Kotlin 1.3) and `fun main(args: Array<String>)`, where `args` holds command-line arguments. Both must be top-level (declared directly in a file, not nested inside another function) and return `Unit`. You don't wrap `main` in a class — unlike Java's `public static void main(String[])`. When compiled for the JVM, the file `App.kt` produces a class `AppKt`, and the compiler generates a synthetic JVM `static main(String[])` bridge so the JVM can launch it. If a file declares both `main()` and `main(args)`, the parameterless one is preferred as the entry point. A program can have only one effective entry point per launch.
code
kotlin · 7 linesfun main(args: Array<String>) {
if (args.isEmpty()) {
println("No arguments given")
} else {
println("Got ${args.size} args: ${args.joinToString()}")
}
}go deeper
Knows both signatures and that main is the start of the program with no class needed.
Explains the JVM bridge (AppKt + synthetic static main) and that args excludes the program name.
Discusses overload preference, @file:JvmName, exitProcess for exit codes, and Unit return semantics.
Frames entry-point design across platforms (JVM/Native/JS), tooling implications, and how build config selects the main class.
## What an entry point is An **entry point** is the single function the runtime calls first to start your program. In Kotlin that function is named `main`. Execution begins at the first line of `main` and the program ends when `main` returns (or an uncaught exception/`exitProcess` stops it). ## The two valid signatures Kotlin accepts exactly two shapes: ```kotlin // 1) No parameters (since Kotlin 1.3) fun main() { println("Hello") } // 2) Receiving command-line arguments fun main(args: Array<String>) { println("First arg: ${args.firstOrNull()}") } ``` - `args: Array<String>` is the list of strings passed on the command line. If you run `java -jar app.jar a b c`, then `args` is `["a", "b", "c"]`. Note `args` does **not** include the program name (unlike C's `argv[0]`). - Both return `Unit` (Kotlin's "no meaningful value" type, like `void`). You normally omit the return type. ## Top-level — no class needed `main` is a **top-level function**: declared directly inside a `.kt` file, not inside a `class` or `object`. This is a deliberate contrast with Java, where `main` must be `public static void main(String[])` inside a class. ```kotlin // File: App.kt — no class anywhere fun main() = println("Runs fine") ``` ## How the JVM finds it The JVM itself can only launch `public static void main(String[])` inside a class. Kotlin bridges this: a file `App.kt` compiles to a JVM class named `AppKt` (file name + `Kt`), and the compiler emits a synthetic static `main` so the JVM has something to call. You can override the generated class name with `@file:JvmName("App")`. ## Picking among overloads If one file declares both `fun main()` and `fun main(args: Array<String>)`, the compiler treats the **parameterless** `main()` as the entry point. Only one entry point is actually invoked per program launch. ## Key keywords/APIs - `fun main` — the entry function. - `Array<String>` — argument type. - `Unit` — implicit return type. - `@file:JvmName` — rename the generated JVM class. - `kotlin.system.exitProcess(code)` — terminate with an explicit exit code.
- Does `args` include the name of the program like `argv[0]` in C?No. Kotlin's `args` contains only the user-supplied arguments; the program/jar name is not element zero.
- What return type does `main` have?`Unit` (effectively `void`). You normally omit it. `main` cannot return an `Int` exit code directly; use `exitProcess(code)` instead.
main is the front door of the program — the runtime always knocks there first.
saying these in an interview costs you the question
- Saying `main` must live inside a class like in Java
- Claiming `args[0]` is the program name
- Thinking `main` can return an Int to set the exit code
- Not knowing the parameterless `fun main()` form exists
- Confusing `Array<String>` with `List<String>` for the parameter