← Chapter 2 — Core Syntax & Data

5. Comments & Java Naming Rules

mixed

Description

Comments explain intent to people reading code. The compiler ignores them, so they should clarify why a choice exists rather than repeat an obvious statement.

Names should describe their purpose. Java convention uses camelCase for variables and methods, PascalCase for classes, and uppercase for constants.

Subtopics

Comment styles

Description

Java supports single-line and block comments.

Explanation

Use comments sparingly and keep them accurate.

Example

// A learner's final score
/* This can span lines */

Identifiers

Description

Names begin with a letter, underscore, or dollar sign.

Explanation

Spaces, keywords, and most punctuation cannot appear in identifiers.

Example

int finalScore = 88;

Explanation

A good name reduces the need for comments. For example, passingScore is easier to understand than x, especially when a program grows.

Example

public class NamingDemo {
    public static void main(String[] args) {
        // Store Rohan's result.
        int finalScore = 88;
        System.out.println(finalScore);
    }
}

Key points

  • Comments are ignored by the compiler.
  • Use meaningful camelCase variable names.
  • Class names begin with an uppercase letter.

Open reference