Unravel โบ Java concepts โบ Language basics
The main method, arguments and console input
public static void main(String[] args) is where a Java program starts.
At a glance
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.
- 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.
- 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.
- 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
- 4The program ends when main returns and all non-daemon threads are finishedJava 21 previews an instance void main() without the ceremony.
- Validate arguments early and print a usage message rather than letting an ArrayIndexOutOfBoundsException escape
- System.exit skips normal cleanup in the calling code (finally blocks do not run) but does run shutdown hooks
- The java launcher loads the class, finds main by its exact signature and calls it on the "main" thread
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); }}- line 4 uses exit, the core of The main method, arguments and console input
- 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
nextIntandnextLineand getting an empty line. - Calling
System.exitinside 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:
- Method calls:
System.exit()nextLine()nextInt()hasNextLine()hasNextInt()System.console()readPassword() - Method names you declare:
main()
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.