๐ŸŽ“ A1Learner
๐Ÿ”
0 of 48 lessons completed

Java Get Started

Getting started with Java is the path from a written file to a running program. The Java compiler turns the file into a form the JVM can run.

The hello program

The file is Main.java and the class is Main. The two names match.

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

A changed message

The message in the code changed from World to Java. The output follows the code.

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, Java!");
  }
}

A two-line program

Each statement ends with a semicolon. The two statements print two lines in order.

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

Example

public class Main {
  public static void main(String[] args) {
    System.out.println("My first Java program");
    System.out.println("It compiled and ran");
  }
}

Key points

  • A Java source file is named Main.java.
  • The class name Main must match the file name Main.java.
  • The javac Main.java command compiles the file.
  • The java Main command runs the compiled program.
  • Each statement ends with a semicolon.
  • Java is case-sensitive, so Main and main mean different names.
  • main is marked static so the JVM can call it without an object.

In this example, the class name Main matches the file name Main.java. The first line prints a title. The second line prints a short report. Each statement ends with a semicolon.

Note

The Class Methods lesson gives static its full pass.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command compiles the file Main.java?