Java Syntax Overview

In the previous chapter, we created a Java file named Main.java and used the following code to display “Hello World” on the screen:

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

Code Breakdown

Every line of code that executes in Java must be contained within a class. In this example, we named the class Main. By convention, a class name should always begin with an uppercase letter.

Note: Java is case-sensitive, meaning that MyClass and myclass are considered different names.

The name of the Java file must match the class name. When saving your file, ensure that the filename matches the class name and has the extension .java. For example, save this file as Main.java. Before running the example on your computer, make sure Java is properly installed. You can refer to the “Get Started” chapter for installation instructions. The expected output is:

Hello World

The main Method

The main() method is essential and appears in every Java program:

public static void main(String[] args)

Any code inside the main() method will be executed. You don’t need to worry about the keywords before and after main right now; you’ll learn about them as you progress through the tutorial.

For now, just remember that every Java program must have a class name that matches the filename, and it must include the main() method.

System.out.println()

Within the main() method, we can use the println() method to print a line of text to the screen.

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

Java Syntax Overview

In the previous chapter, we created a Java file named Main.java and used the following code to display “Hello World” on the screen:

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

Code Breakdown

Every line of code that executes in Java must be contained within a class. In this example, we named the class Main. By convention, a class name should always begin with an uppercase letter.

Note: Java is case-sensitive, meaning that MyClass and myclass are considered different names.

The name of the Java file must match the class name. When saving your file, ensure that the filename matches the class name and has the extension .java. For example, save this file as Main.java. Before running the example on your computer, make sure Java is properly installed. You can refer to the “Get Started” chapter for installation instructions. The expected output is:

Hello World

The main Method

The main() method is essential and appears in every Java program:

public static void main(String[] args)

Any code inside the main() method will be executed. You don’t need to worry about the keywords before and after main right now; you’ll learn about them as you progress through the tutorial.

For now, just remember that every Java program must have a class name that matches the filename, and it must include the main() method.

System.out.println()

Within the main() method, we can use the println() method to print a line of text to the screen.

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