Skip to content

Java Basic Syntax

Before diving into Java's advanced features, mastering its basic syntax is crucial. This chapter will introduce a basic Java program structure and explain its key components, such as case sensitivity, class names, method names, and keywords.

First Java Program: Hello, World!

Let's start with the traditional beginner program from the programming world—outputting "Hello, World!". Through this simple example, we can understand what a complete Java program looks like.

java
// This is a simple Java program
// The filename must be HelloWorld.java

public class HelloWorld {
    
    // The main method is the entry point of the program
    public static void main(String[] args) {
        // Use System.out.println() to print text to the console
        System.out.println("Hello, World!"); 
    }
    
}

To compile and run this program, you can:

  1. Save the code as a file named HelloWorld.java.
  2. Open a terminal or command prompt and navigate to the directory where the file is located.
  3. Compile: Run javac HelloWorld.java. This will generate a bytecode file named HelloWorld.class.
  4. Run: Run java HelloWorld. The console will output Hello, World!.

Java Program Basic Structure Analysis

Now, let's break down the above code line by line:

  • public class HelloWorld:

    • The class keyword is used to declare a class. In Java, all code must reside within a class.
    • HelloWorld is the name of the class we defined. In Java, the name of a public class must exactly match the filename (including case). Therefore, this file must be named HelloWorld.java.
    • public is an access modifier, meaning this class can be accessed by any other class.
  • public static void main(String[] args):

    • This is the program's main (main) method. When you run a Java program, the JVM starts execution from this method. It is the entry point for all Java applications.
    • public: Means this method can be called from outside the class.
    • static: Indicates this method belongs to the HelloWorld class itself, not to a specific instance (object) of the class. This allows the JVM to call it without creating a HelloWorld object.
    • void: Indicates this method does not return any value.
    • main: Is the standard name for this method.
    • String[] args: This is the method's parameter. It's an array of strings that allows you to pass arguments from the command line when running the program.
  • System.out.println("Hello, World!");:

    • This is the core statement that performs the output.
    • System is a built-in final class located in the java.lang package.
    • out is a static member variable of the System class, which is an object of the PrintStream class.
    • println() is a method of the PrintStream class that prints the string passed to it and adds a newline character at the end.
    • Note: Every statement in Java must end with a semicolon (;).

Key Syntax Rules

  1. Case-Sensitive

    • Java is case-sensitive. This means HelloWorld and helloworld are two completely different identifiers. Keywords (like public, class) must also use lowercase.
  2. Identifiers

    • Identifiers are names used to name classes, methods, variables, etc.
    • All identifiers must begin with a letter (A-Z or a-z), dollar sign ($), or underscore (_).
    • After the first character, identifiers can contain letters, dollar signs, underscores, or digits (0-9).
    • Keywords cannot be used as identifiers.
  3. Keywords

    • Keywords are reserved words in the Java language with special meanings. Examples include class, public, static, void, if, else, for, while, etc. You cannot use these words as variable names or class names.
  4. Naming Conventions

    • Following standard naming conventions makes your code easier to read and understand. These are not mandatory syntax rules, but best practices followed by all Java developers.
    • Class names: Use "Upper CamelCase", where the first letter of each word is capitalized. Examples: HelloWorld, MyFirstProgram.
    • Method and variable names: Use "lower CamelCase", where the first letter of the first word is lowercase and the first letters of subsequent words are capitalized. Examples: myVariable, calculateSum().
    • Constant names: All letters are uppercase, with words separated by underscores. Examples: MAX_VALUE, PI.

Mastering these basic syntax rules is the first step to writing any Java program. In the following chapters, we will build more complex concepts based on this knowledge.

Content is for learning and research only.