Java Syntax



Java Syntax

In the Java Hello World chapter, we created a Java file called HelloWorld.java, and we used the following code to output Hello World! on the screen:

HelloWorld.java :

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

Example explained

Every code that runs in Java must be inside a class. In our example, we named the class HelloWorld. In Java, a class should always start with an uppercase first letter.

Notes:

  • Java programming language is case-sensitive, meaning "HelloWorld" and "helloworld" are different.
  • In Java, the class name and the java file name must be the same. When saving a file, we need the class name and add .java to the end of the filename.

The output of the above example can be as follows:

Hello World!

The main Method

The main() method is required in every Java program, and it is the entry point for a program.

public static void main(String[] args)

Any code within the main() method will be executed. For now, you do not have to understand the keywords before and after main. We will explain them along with this tutorial.

To sum up, here are the things to remember:

  • Every Java program has a class name that must match the filename.
  • Every Java program must contain the main() method.

System.out.println()

Inside the main() method, we used the println() method to print a text to the screen.

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

Notes:

  • The curly braces {} designate the start and the end of a block of code.
  • In Java, each code statement must end with a semicolon ;.


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.