Java ยท Chapter 3 of 42

Java Get Started

Every Java application has a class containing a main method, which is the entry point of the program. The method signature `public static void main(String[] args)` is fixed and must be written exactly this way.

Java is a strongly typed, compiled language: source code (.java) is compiled to bytecode (.class), which the JVM then interprets or JIT-compiles into machine instructions.

Syntax
public class Main {
  public static void main(String[] args) {
    // code goes here
  }
}

Anatomy of a Java program

A Java file contains at least one class. The class containing main() is where execution starts. Statements end with semicolons and blocks use curly braces.

Compiling and running

Save your file as ClassName.java (must match the public class name), compile with javac, then run with java.

Example 1 (java)
public class Main {
  public static void main(String[] args) {
    System.out.println("My First Java Program");
  }
}
Output
My First Java Program

The main method is the entry point of every Java application.

Key points

  • Every Java app needs a main method.
  • The file name must match the public class name.
  • Statements end with semicolons.
  • Java is case-sensitive.
๐Ÿ’ก Note: Forgetting to match the file name to the public class name causes a compile error.

๐Ÿ“ Quick Quiz

1. What must the file name match?

2. What is the entry point of a Java app?

3. Is Java case-sensitive?