Java Syntax

Understanding Java language rules and structure

📝 Java Syntax Rules

Java syntax defines the rules for writing Java programs. It includes proper structure, naming conventions, keywords, and punctuation that make your code readable and executable.


// Basic Java syntax example
public class SyntaxExample {
    public static void main(String[] args) {
        String message = "Learning Java syntax!";
        System.out.println(message);
    }
}
                                    

Output:

Learning Java syntax!

Key Syntax Elements

🏷️

Class Declaration

Every Java program starts with a class

public class MyClass {
    // code here
}
🚀

Main Method

Entry point of Java programs

public static void main(String[] args) {
    // program starts here
}
;

Semicolons

End every statement with semicolon

int age = 25;
String name = "John";
{}

Curly Braces

Define code blocks and scope

if (condition) {
    // code block
}

🔹 Java Program Structure

Every Java program follows this basic structure:

// 1. Package declaration (optional)
package com.example;

// 2. Import statements (optional)
import java.util.Scanner;

// 3. Class declaration (required)
public class ProgramStructure {
    
    // 4. Class variables (optional)
    static String appName = "My App";
    
    // 5. Main method (required for executable programs)
    public static void main(String[] args) {
        // 6. Local variables
        int number = 42;
        String text = "Hello Java";
        
        // 7. Program logic
        System.out.println(appName);
        System.out.println(text + " " + number);
    }
}

Output:

My App

Hello Java 42

🔹 Naming Conventions

Java follows specific naming rules:

🔸 Classes

Use PascalCase (first letter uppercase)

public class StudentRecord { }
public class BankAccount { }
public class CarEngine { }

🔸 Variables and Methods

Use camelCase (first letter lowercase)

int studentAge = 20;
String firstName = "John";
double accountBalance = 1500.50;

public void calculateTotal() { }
public String getUserName() { }

🔸 Constants

Use UPPER_CASE with underscores

final int MAX_SIZE = 100;
final String DATABASE_URL = "localhost";
final double PI = 3.14159;

🔹 Comments in Java

Use comments to explain your code:

// Single-line comment
// This explains what the next line does

/*
 * Multi-line comment
 * Used for longer explanations
 * Can span multiple lines
 */

/**
 * Documentation comment (Javadoc)
 * Used to generate API documentation
 * @param args command line arguments
 */
public static void main(String[] args) {
    int age = 25;        // Variable to store age
    
    /* 
     * Print welcome message
     * This is visible to users
     */
    System.out.println("Welcome! You are " + age + " years old.");
}

Output:

Welcome! You are 25 years old.

🔹 Case Sensitivity

Java is case-sensitive - capitalization matters:

❌ These are different:

String name = "John";
String Name = "Jane";    // Different variable
String NAME = "Bob";     // Different variable

// Method names are also case-sensitive
System.out.println();    // Correct
system.out.println();    // Error - 'system' not found

✅ Be consistent:

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);

🔹 Java Keywords

Reserved words that have special meaning:

🔷 Access Modifiers

public private protected

🔷 Data Types

int double boolean String

🔷 Control Flow

if else for while

🔷 OOP Keywords

class extends implements

🧠 Test Your Knowledge

Which symbol is used to end a statement in Java?