Stop 01 · Java FoundationsTopics · 4/7

Your First Program & main()

Part of the Java Roadmap

In short

Every Java program you will ever write, from a calculator to a banking backend, starts at the same line: public static void main. Understanding each word in it takes the mystery out of Java.

Every Java application starts from public static void main(String[] args) inside a class. Java is case-sensitive, statements end with semicolons and the file name must match the public class name.

The jaadu dressed as a school watchman swings open a big iron main gate as a line of tiny paper figures walks in.

Think of it like this

The main gate of a school

A school may have many classrooms, but on the first day everyone enters through the main gate where the watchman lets them in. main is that gate. The JVM looks for exactly this method and walks in there first; everything else happens because main calls it.

Watch it happen

What the JVM does with your first program

Find class
Find main
Run lines
Print output
Exit

1/5You ask the JVM to run the class HelloIndia.

The big ideas

What you need to know

Code lives in a class

A public class must be in a file with the same name: HelloIndia goes in HelloIndia.java.

Every word in main matters

public so the JVM can reach it, static so no object is needed, void because it returns nothing, String[] args for command-line inputs.

Case-sensitive

Main, MAIN and main are three different names. System must have a capital S.

Semicolons end statements

Each statement ends with ;. Braces { } group lines into blocks.

See it in code

Walk through the code

java
1public class HelloIndia {2 3    public static void main(String[] args) {4        System.out.println("Namaste, Java!");5        System.out.println("2 + 3 = " + (2 + 3));6 7        if (args.length > 0) {8            System.out.println("Hi, " + args[0]);9        }10    }11}

1/4The class name matches the file name exactly.

Save as HelloIndia.java. Run it with an argument, like java HelloIndia.java Priya, to see args in action.

Save yourself the pain

Common mistakes

Oops

Saving public class HelloIndia in a file called hello.java.

Do this instead

The file name must match the public class name exactly, including capitals: HelloIndia.java.

Oops

Writing "2 + 3 = " + 2 + 3 and expecting 5.

Do this instead

Java reads left to right, so text + 2 becomes text, then + 3 is joined too: "2 + 3 = 23". Use brackets: (2 + 3).

Oops

Using single quotes for text, like 'Hello'.

Do this instead

Double quotes make a String. Single quotes are for one char, like 'A'.

Your mission

Your own introduction

~15 min

Quick check

Why is main declared static?

Go deeper

  1. 1Write Hello World and explain each word of the main method signature.
  2. 2Print command-line arguments from the args array.
  3. 3Break the program on purpose (missing semicolon, wrong file name) and read the compiler errors.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge