Java Basic Syntax


Java Basic Syntax

In this tutorial, we will learn the basic syntax of Java language. We will go through the key components of a simple Java program.

Java Program

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

Output

Hello, World!

Basic Syntax of a Java Program

  1. public class HelloWorld
    This line declares a public class named HelloWorld. In Java, every application must contain at least one class definition, which encloses the program's entry point.
  2. public static void main(String[] args)
    This line defines the main method, which is the entry point of the program. public means the method is accessible from outside the class, static means it can be called without creating an instance of the class, void means it does not return a value, and String[] args is an array of strings that stores arguments passed to the program.
  3. {
    This opening brace marks the beginning of the main method's body.
  4. System.out.println("Hello, World!");
    This line prints the string "Hello, World!" to the standard output (usually the screen). System.out is an output stream, and println is a method that prints the provided string followed by a new line.
  5. }
    This closing brace marks the end of the main method's body.
  6. }
    This closing brace marks the end of the HelloWorld class definition.

Key Points to Remember

  • All Java statements must end with a semicolon (;).
  • The main method is the entry point of a Java program.
  • Comments can be added using // for single-line comments or /* ... */ for multi-line comments.
  • Code blocks are enclosed in curly braces {}.
  • Java is case-sensitive, meaning that Main and main would be considered different identifiers.