Hello World and console output

Hello World and console output

A first program should show a recognizable result. Kotlin uses fun to declare a function, and a console program commonly starts in main. Braces contain its body. A string is text inside double quotes, and println writes a value followed by a newline.

Statements inside main run in their written order unless control flow changes that order. Separate lines usually need no semicolons. Names are case-sensitive: println is different from Println. A line comment beginning with // explains source without executing an operation.

What you’ll learn

  • Trace main and two output statements.
  • Recognize strings, comments and case-sensitive names.

Hello World and console output in the example

  1. Enter main

    A first program should show a recognizable result.

    Enter main
  2. Print first line

    Statements inside main run in their written order unless control flow changes that order.

    Print first line
  3. Print second line

    The original Hello, World! greeting prints first.

    Print second line
The original Hello, World! greeting prints first. The second statement adds the school message on its own line. Main returns after both statements; it does not need an explicit returned value for this Unit-returning example.

Follow hello world and console output

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  println("Hello, World!")
  println("Welcome to River School")
}

Expected output

Hello, World!
Welcome to River School

Follow the execution

The original Hello, World! greeting prints first. The second statement adds the school message on its own line. Main returns after both statements; it does not need an explicit returned value for this Unit-returning example.

Try this in the Kotlin compiler by pasting the complete program.

Practice: Hello World and console output

Your task: How do you keep only the school greeting?

Kotlin/JVM · complete program

fun main() {
  println("Hello, World!")
  println("Welcome to River School")
}

Worked answer

Remove the first println and keep the second. The remaining statement prints Welcome to River School once.

Hello World and console output: the important boundary

Console text is not an Android screen. The existing output_1 and output_ bookmarks remain available in the example. The filename can be flexible, but build tools and generated JVM names follow their own conventions.

Check your understanding

How do you keep only the school greeting?
Choose one answer. You can try again.

Answer and explanation

Remove the first println and keep the second.

Remove the first println and keep the second. The remaining statement prints Welcome to River School once.

Hello World and console output: what to keep in mind

Remove the first println and keep the second. The remaining statement prints Welcome to River School once. Console text is not an Android screen.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Variables, val and var →