Your first Java program

Every Java program is a class with a method the JVM can call. The smallest one is six lines, and each of them does something: the class gives the file its name, the method gives the JVM an entry point, and the statement writes a line. This lesson writes it, runs it, reads the exit status, and then breaks it in the way a beginner breaks it first, so that the diagnostic is not a surprise later.

The program

Save this as Main.java. Two statements, one per line, so the output has two lines and the order is the order of the program.

public class Main { public static void main(String[] args) { System.out.println("first"); System.out.println("second"); } }

javac -Xlint:all Main.java
java Main

first
second

The compiler printed nothing, which with -Xlint:all means it found nothing to say. The JVM printed the two lines in the order the statements are written, because a program runs from the first statement of main to the last, one at a time.

The public class and the file name

public class Main declares a class named Main that any other code may use. The compiler checks the name against the file name: a public class must live in a file named after it. Save the same class in Other.java and the build stops before it reads the body:

public class Main { public static void main(String[] args) { System.out.println("wrong file name"); } }

javac -Xlint:all Other.java

Other.java:1: error: class Main is public, should be declared in a file named Main.java
public class Main {
^
1 error

The rule has one exception and one reason. A class that is not public may live in any file, and a file may hold several non-public classes. The reason is the class file: the compiler writes Main.class, the JVM looks a class up by its name, and a file name other than the class name would make the source and the output disagree.

The main method

public static void main(String[] args) is the method the JVM calls when the class is started. Every word matters:

public lets the launcher, which is outside the class, call it. static means the JVM calls it without creating an object of the class. void means it returns no value to its caller. String[] args is the array of words typed after the class name, empty when there are none. Change any one of them and the class compiles but the launcher refuses to start it.

The name args is a convention, not a rule: the parameter can be called anything, and the launcher only looks at the types. The course keeps args everywhere, as the JDK documentation does.

System.out.println

System is a class of the standard library, out is a static field of it that holds the standard output stream, and println is a method of that stream that writes its argument and a line separator. The three parts are joined by dots because each one is a member of the one before it.

print writes without the separator, and the next output continues on the same line. Both take a string, a number, a character, a boolean or an object; an object is printed by calling its toString method, which every class inherits.

public class Pieces { public static void main(String[] args) { System.out.print("one "); System.out.print("two"); System.out.println(); System.out.println("total: " + (2 + 3)); } }

javac -Xlint:all Pieces.java
java Pieces

one two
total: 5

The empty println() ends the line that the two print calls left open. The parentheses around 2 + 3 matter: without them the + would join the string and the first number before adding the second, which is the subject of lesson 010.

Build, run and the exit status

A program that ends normally returns status 0 to the shell, and a program that calls System.exit(2) returns 2. The shell keeps the status of the last command in $?, which is how a script decides whether the program worked:

javac -Xlint:all Main.java
java Main
echo "status: $?"

first
second
status: 0

The build line is repeated because the commands are the ones that were run, in order. A status other than 0 from a Java program means either an uncaught exception, which prints a stack trace and returns 1, or an explicit System.exit with a number the program chose. Lesson 031 uses the second on purpose.

A missing semicolon

Delete the semicolon at the end of the first statement inside main and build again. The compiler does not guess where the statement ends; it reads the next line as part of the same statement, finds a name where it expected an operator, and points at the first token it cannot use:

public class Broken { public static void main(String[] args) { int total = 0 System.out.println(total); } }

javac -Xlint:all Broken.java

Broken.java:3: error: ';' expected
int total = 0
^
1 error

Read the three lines in order. The first names the file, the line and the kind of problem. The second is the source line, and the caret under it points at the end of that line, which is where the parser noticed. The last line is the count, and the shell sees status 1. The next lesson reads diagnostics of this shape in full, warnings included.

Exercises

  1. Add a third println to Main.java and predict the three output lines before you build it.
  2. Rename the class to Hello and build the file without renaming it. Which error do you get, and at which line?
  3. Fix Broken.java by adding one character, then build and run it.
  4. Run java Main one two. Does the output change, and why not?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which method does the JVM call when a class is started?

    Answer

    public static void main(String[] args)

    The launcher looks for a public static main method that takes an array of strings and returns nothing.

  2. A file holds public class Report. What must the file be called?

    Answer

    Report.java

    A public class must be declared in a file named exactly after it, case included.

  3. What does static mean on the main method?

    Answer

    The JVM calls it without creating an object of the class

    A static method belongs to the class, so the launcher calls it on the class itself.

  4. What does this program print? public class Q1 { public static void main(String[] args) { System.out.println("first"); System.out.println("second"); } }

    Answer

    first / second

    Each println ends its line, so the two words are on two lines.

  5. What does this program print? public class Q2 { public static void main(String[] args) { System.out.print("one "); System.out.print("two"); } }

    Answer

    one two

    print writes no line separator, so the two calls produce one line with a space between the words.

  6. What does this program print? public class Q3 { public static void main(String[] args) { System.out.println("total: " + (2 + 3)); } }

    Answer

    total: 5

    The parentheses add the numbers first, and the string is joined to the result.

  7. What is the first line of the compiler output for this program? public class Q4 { public static void main(String[] args) { int total = 0 System.out.println(total); } }

    Answer

    error: ';' expected

    The statement has no semicolon, and javac reports the missing token at the end of that line.

  8. What exit status does the shell see after this program runs? public class Q5 { public static void main(String[] args) { System.out.println("done"); } }

    Answer

    0

    A program that reaches the end of main returns 0 to the shell.

  9. What does String[] args hold?

    Answer

    The words typed after the class name

    The launcher copies the words after the class name into the array, one element per word.

  10. What does this program print? public class Q6 { public static void main(String[] args) { System.out.println("a"); System.out.print("b"); System.out.println("c"); } }

    Answer

    a / bc

    The first println ends its line, the print leaves b open, and the last println adds c and the separator.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev