Skillversitas

Basic Syntax

Java uses curly braces {} to define code blocks, unlike Python which uses indentation. That said, consistent indentation is still important for code readability. The standard Java community convention uses 4 spaces for each indentation level (not tabs).

Java is case-sensitive — name, Name, and NAME are three different variables. Every statement ends with a semicolon ;. Single-line comments use //, multi-line comments use /* ... */, and documentation uses /** ... */ (Javadoc).

Naming conventions in Java follow the Java Naming Conventions: variables and methods use camelCase (lowercase first letter), classes use PascalCase (uppercase first letter), constants use UPPER_SNAKE_CASE, and packages use all lowercase with no spaces.

Java Syntax Rules:

  • Java is case-sensitive
  • Every statement ends with a semicolon ;
  • Code blocks are delimited by curly braces {}
  • Standard indentation is 4 spaces
  • Single-line comments: //
  • Multi-line comments: /* ... */
  • Documentation: /** ... */

Code Example

public class BasicSyntax {
    public static void main(String[] args) {
        // Single-line comment

        /*
         * Multi-line comment
         * that can span
         * several lines of text
         */

        String name = "Java";
        double version = 21.0;

        if (version > 17) {
            System.out.println(name + " version " + version);
            System.out.println("Curly braces are required!");
        }
    }
}

Code Explanation

The first line is a single-line comment — all text after // is ignored by the compiler. The /* ... */ block is a multi-line comment that is useful for documentation. The name variable is of type String and version is of type double. The if (version > 17) structure checks a condition; if it is true, the code block inside the curly braces is executed. Notice that both lines inside the if block have the same indentation (4 spaces). Strings are joined with the + operator (concatenation).

Everyday Example

You build an exam score checker program. A student passes if their score >= 70:

public class GradeChecker {
    public static void main(String[] args) {
        String studentName = "John";
        int score = 85;

        if (score >= 70) {
            System.out.println(studentName + " has PASSED");
        } else {
            System.out.println(studentName + " has NOT PASSED");
        }
    }
}

Summary

Basic Java syntax is similar to C/C++, but simpler and safer. The combination of curly braces for code blocks, semicolons as statement terminators, and consistent naming conventions makes Java code easy to read and maintain. Master these rules before moving on to the next topic.