Lesson 8 of 50 – Java Syntax
16%

Java Syntax

Java syntax refers to the rules used to write valid Java programs. These rules define how we write classes, methods, statements, variables, and expressions.

Learning Objective: Understand Java classes, the main method, statements, semicolons, braces, identifiers, comments, and case sensitivity.
1. Basic Structure of a Java Program

Every basic Java program can contain a class and a main method.

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

Output:

Hello Java
  • public class Hello declares a class.
  • main() is the application's entry point.
  • System.out.println() displays output.
  • Curly braces define blocks of code.
2. Java Classes

A class is a blueprint used to define objects. It can contain variables, constructors, and methods.

public class Student {
    String name;
    int age;
}

In this example:

  • class is a Java keyword.
  • Student is the class name.
  • name and age are instance variables.
If a top-level class is declared public, its filename must match the class name. For example, save this class in Student.java.
3. The main() Method

The main method is the standard starting point for launching a simple Java application.

public static void main(String[] args) {
    System.out.println("Welcome");
}
  • public — makes the method accessible.
  • static — allows invocation without creating an instance of the class.
  • void — indicates that the method returns no value.
  • main — the method name recognized by the Java launcher.
  • String[] args — stores command-line arguments.
4. Java Statements

A statement is an instruction that tells Java to perform an operation.

int age = 20;
System.out.println(age);
age = 25;
System.out.println(age);

Output:

20
25

The statements declare a variable, display its value, update the variable, and display the updated value.

5. Semicolons in Java

Most Java statements end with a semicolon (;).

int number = 10;
System.out.println(number);

Output:

10

Incorrect example:

int number = 10
System.out.println(number);

The first statement is missing a semicolon, so the compiler reports a syntax error.

6. Curly Braces in Java

Curly braces { } group statements into blocks. They are commonly used with classes, methods, loops, and conditional statements.

public class Example {
    public static void main(String[] args) {
        if (10 > 5) {
            System.out.println("Ten is greater than five");
        }
    }
}

Output:

Ten is greater than five

Make sure every opening brace has a matching closing brace.

7. Java Is Case-Sensitive

Java distinguishes between uppercase and lowercase letters.

int age = 20;
int Age = 30;

System.out.println(age);
System.out.println(Age);

Output:

20
30

Here, age and Age are two different variables.

Write Java keywords correctly. For example, public is valid, but Public is not the same keyword.
8. Java Identifiers

Identifiers are names given to classes, variables, methods, and other program elements.

Valid identifiers:

studentName
age
total_marks
number1
_myVariable

Invalid identifiers:

2number
student-name
class
  • An identifier cannot begin with a digit.
  • Spaces and hyphens are not allowed in ordinary identifiers.
  • Java keywords cannot be used as ordinary identifiers.
  • Names are case-sensitive.
  • Underscores are allowed in identifiers.
9. Java Keywords

Keywords are reserved words with special meanings in Java.

Keyword Purpose
class Declares a class.
int Declares an integer variable.
if Starts a conditional statement.
else Defines an alternative conditional branch.
static Declares a class-level member.
return Returns from a method and may provide a value.

Keywords have predefined meanings and cannot be used as ordinary variable or class names.

10. Comments in Java

Comments explain code and are ignored by the compiler as executable instructions.

Single-line comment:

// This is a single-line comment
System.out.println("Hello Java");

Multi-line comment:

/*
This is a multi-line comment.
It can span several lines.
*/
System.out.println("Java Programming");

Documentation comment:

/**
 * Displays a welcome message.
 */
public static void welcome() {
    System.out.println("Welcome");
}
11. Java Naming Conventions

Naming conventions help make Java code easier to read and maintain.

Element Common Convention Example
Class PascalCase StudentDetails
Variable camelCase studentName
Method camelCase calculateTotal()
Constant Uppercase with underscores MAX_VALUE

These are conventions, not strict syntax rules. Following them improves readability.

12. Java Syntax Errors

Syntax errors occur when source code violates Java's language rules.

Incorrect code:

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

The print statement is missing a semicolon.

Correct code:

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

Output:

Hello

Read compiler messages carefully to find the location and type of an error.

13. Practice Program and Summary

Create a Java program that declares a name and age, then displays both values.

public class Student {
    public static void main(String[] args) {
        String studentName = "Rahul";
        int age = 20;

        System.out.println("Name: " + studentName);
        System.out.println("Age: " + age);
    }
}

Output:

Name: Rahul
Age: 20

Key Points:

  • Java syntax defines the rules for writing programs.
  • Classes organize Java code.
  • The main method is the standard application entry point.
  • Most statements end with semicolons.
  • Curly braces define code blocks.
  • Java identifiers are case-sensitive.
  • Comments explain code without executing as instructions.
You have completed the Java Syntax lesson. Practice these rules before moving to the next lesson.

🧠 Quick Quiz

1. Which symbol usually ends a Java statement?




2. Is Java case-sensitive?




3. Which is a valid Java identifier?