skip to content

fun main & Program Entry

Where a Kotlin program starts: a top-level fun main(), with or without an args array, and no enclosing class required. The follow-up worth knowing is suspend fun main, which lets the entry point itself be a coroutine.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

What is the entry point of a Kotlin program, and what are the valid signatures for `main`?

level: juniorimportance: must knowfreq 75%

answer

  1. main = where the program starts
  2. Two forms: main() or main(args: Array<String>)
  3. Top-level, no class required
  4. args excludes the program name
  5. Compiles to AppKt with synthetic static main

basics

~10 s

Every 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 s

Kotlin'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 lines
kotlin
fun main(args: Array<String>) {
    if (args.isEmpty()) {
        println("No arguments given")
    } else {
        println("Got ${args.size} args: ${args.joinToString()}")
    }
}

go deeper

for a junior

Knows both signatures and that main is the start of the program with no class needed.

for a middle

Explains the JVM bridge (AppKt + synthetic static main) and that args excludes the program name.

for a senior

Discusses overload preference, @file:JvmName, exitProcess for exit codes, and Unit return semantics.

for a principal

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

context

open as a page

How do you read command-line arguments and set a process exit code from a Kotlin `main`?

level: middleimportance: should knowfreq 45%

basics

~10 s

Command-line arguments arrive as the args: Array<String> parameter of main. To end the program with a specific status code, call kotlin.system.exitProcess(code) — for example exitProcess(1) to signal failure.

open as a page

What is `suspend fun main` and when would you use it?

level: middleimportance: should knowfreq 55%

basics

~20 s

suspend fun main lets the program's entry point call suspending coroutine functions directly, without manually creating a runBlocking block. The runtime starts a coroutine for you and waits until it finishes before the program exits.

open as a page

How does the JVM locate and invoke a Kotlin top-level `main`, given the JVM only knows `public static void main(String[])` inside a class?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Kotlin compiles a file like App.kt into a JVM class named AppKt and generates a hidden static main(String[]) method inside it. The JVM launches that class, and that generated method calls your real top-level main.

open as a page

Compare Kotlin's program-entry model to Java's, and discuss design implications of allowing a top-level / expression-body `main`.

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Java forces main to be a static method inside a class; Kotlin lets it be a free top-level function and even a one-line expression body. This reduces ceremony, but you must point your tooling at the generated file class instead of a function name.

open as a page