Skip to content

Lesson 1: Java Syntax and Structure

Lesson 1: Java Syntax and Structure

Overview:

In Lesson 1, we will explore the fundamental syntax rules and the structural components of a Java program. Understanding the basic syntax is crucial for writing clear and effective code, and familiarity with the structure of a Java program is essential for organizing and executing your code.

Basic Syntax Rules in Java:

  1. Case Sensitivity:
    • Java is case-sensitive. This means that variables, method names, and other identifiers must be consistently written in the same case throughout the code.
int myVariable;
int MyVariable; // Different from myVariable

Semicolons:

  • Statements in Java end with a semicolon (;). It indicates the end of a statement.
int x = 5;
System.out.println("Hello, World!");

Comments:

  • Comments are used for documentation and are ignored by the compiler.
  • Single-line comments start with //, and multi-line comments are enclosed between /* and */.
// This is a single-line comment

/*
This is a
multi-line comment
*/

Variables:

  • Variables are containers for storing data.
  • They must be declared with a data type before use.
int age = 25;
double price = 19.99;
String name = "John";

Data Types:

  • Java has various data types, including int, double, boolean, char, etc.
  • Data types specify the size and type of values that can be stored.
int count = 10;
double average = 5.5;
boolean isJavaFun = true;
char grade = 'A';

Whitespace:

  • Java ignores spaces, tabs, and newlines. They are used to make the code more readable.
int x = 5;  // This is valid
int   y = 10; // This is also valid

Structure of a Java Program:

A basic Java program consists of the following components:

// This is a single-line comment

/*
This is a
multi-line comment
*/

// Class Declaration
public class HelloWorld {

    // Main Method (Entry Point of the Program)
    public static void main(String[] args) {

        // Statements and Code Blocks
        System.out.println("Hello, World!");

    } // End of main method

} // End of class
  • Class Declaration:
    • The program’s code is enclosed within a class.
    • The keyword class is followed by the class name (e.g., HelloWorld).
  • Main Method:
    • The main method is the entry point of the program. It is where the program starts execution.
    • It has a specific signature: public static void main(String[] args).
    • The statements within the main method are executed when the program runs.
  • Statements and Code Blocks:
    • Statements are individual instructions terminated by semicolons.
    • Code blocks are enclosed within curly braces {} and group multiple statements. They define the scope of variables and control flow structures.

This structure provides a foundation for Java programs.

This excerpt provides an overview of basic Java syntax rules and the structure of a simple Java program. It includes comments, variable declarations, the main method, and basic statements to print a greeting.

// Welcome to Java Programming!

/*
Java is a versatile and powerful programming language
known for its readability and portability.
This is a simple Java program to print "Hello, World!"
*/

// Class Declaration
public class HelloWorld {

    // Main Method (Entry Point of the Program)
    public static void main(String[] args) {

        // Basic Syntax Rules

        // Case Sensitivity
        int age = 25;
        double price = 19.99;
        String name = "John";

        // Semicolons
        System.out.println("Hello, " + name + "!"); // Print using concatenation

        // Comments
        // This is a single-line comment

        /*
        This is a
        multi-line comment
        */

        // Data Types
        boolean isJavaFun = true;
        char grade = 'A';

        // Whitespace
        int x = 5;  // This is valid
        int   y = 10; // This is also valid

        // Program Structure

        // Class Declaration
        // The keyword 'class' is followed by the class name (HelloWorld)
        // Everything within the curly braces defines the class scope
        // and contains the program's code
        public class HelloWorld {

            // Main Method
            // The 'main' method is the entry point of the program
            // It has a specific signature: public static void main(String[] args)
            // The statements within the main method are executed when the program runs
            public static void main(String[] args) {

                // Statements and Code Blocks
                System.out.println("Hello, World!");

            } // End of main method

        } // End of class

    } // End of main method

} // End of class