BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Language basics

Java language

The main method, arguments and console input

public static void main(String[] args) is where a Java program starts.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

The front door of a shop: the program always enters through the door named main, and what the customer says on the way in is args.

What happens, in order
  1. 1Run java App alice 3 and args[0] is "alice", args[1] is "3" (always text, convert with Integer.parseInt)args.length tells you how many were given.
  2. 2Scanner in = new Scanner(System.in); String line = in.nextLine(); int n = in.nextInt(); reads inputAfter nextInt() call nextLine() once to consume the rest of the line.
  3. 3System.out is standard output, System.err is for errors, and System.exit(1) ends the program with an exit code (0 means success) that scripts and CI look at
  4. 4The program ends when main returns and all non-daemon threads are finishedJava 21 previews an instance void main() without the ceremony.
Remember
Watch out
Not converting the text argumentsMixing nextInt and nextLine and getting an empty lineCalling System.exit inside library code or tests
Example (highlighted lines explain it)
import java.util.Scanner;public class Greeter {  public static void main(String[] args) {    if (args.length < 1) { System.err.println("usage: Greeter <name>"); System.exit(2); }    Scanner in = new Scanner(System.in);    System.out.println("Hello " + args[0] + ", enter a number:");    int n = in.nextInt();    System.out.println(n * 2);  }}
  1. line 4 uses exit, the core of The main method, arguments and console input
  2. line 7 uses nextInt, the core of The main method, arguments and console input
Read the full explanation (about 1 min)

public static void main(String[] args) is where a Java program starts. args holds the command-line arguments, System.out prints, and Scanner(System.in) reads what the user types.

A mental model

The front door of a shop: the program always enters through the door named main, and what the customer says on the way in is args.

How it works

Run java App alice 3 and args[0] is "alice", args[1] is "3" (always text, convert with Integer.parseInt). args.length tells you how many were given.

Scanner in = new Scanner(System.in); String line = in.nextLine(); int n = in.nextInt(); reads input. After nextInt() call nextLine() once to consume the rest of the line.

System.out is standard output, System.err is for errors, and System.exit(1) ends the program with an exit code (0 means success) that scripts and CI look at.

The program ends when main returns and all non-daemon threads are finished. Java 21 previews an instance void main() without the ceremony.

Going deeper

Validate arguments early and print a usage message rather than letting an ArrayIndexOutOfBoundsException escape. For anything beyond two or three options use a library such as picocli.

System.exit skips normal cleanup in the calling code (finally blocks do not run) but does run shutdown hooks. Do not call it from library code.

Exit codes are the contract with the shell: 0 success, non-zero failure. An uncaught exception in main gives exit code 1 and prints the stack trace.

Under the hood

The java launcher loads the class, finds main by its exact signature and calls it on the "main" thread. Standard streams are static fields of System (in, out, err) that can be redirected with System.setOut.

Common mistakes

  • Not converting the text arguments.
  • Mixing nextInt and nextLine and getting an empty line.
  • Calling System.exit inside library code or tests.

How Unravel recognises it

When you paste code, Unravel looks for these signs of the main method, arguments and console input. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is The main method, arguments and console input?

public static void main(String[] args) is where a Java program starts. args holds the command-line arguments, System.out prints, and Scanner(System.in) reads what the user types.

What are the most common mistakes with The main method, arguments and console input?

Not converting the text arguments. Mixing nextInt and nextLine and getting an empty line. Calling System.exit inside library code or tests.

How can I see The main method, arguments and console input in real code?

Paste your own Java into Unravel and it will point out where the main method, arguments and console input appears, explain how it works at the detail level you choose, and draw the flow of the code around it.