1.3 Basic Syntax Rules for Java
Prerequisites: Line-by-line Understanding of Java Code
Estimated time: 15 minutes
You are ready to continue when: You can fix parentheses, semicolons, capitalization, and filename issues based on the compiler's location
Rule 1: Delimiters Must Be Paired
Hello World uses these delimiters:
public class HelloWorld { // { With }
public static void main(String[] args) { // ( With ), [ With ]
System.out.println("Hello World!"); // ( With ), " With "
}
}{}surrounds class bodies, method bodies, and other code blocks;()surrounds parameter lists or called parameters;[]is part of an array type or array access;- paired double quotes surround string literals.
When structures are nested, the delimiter opened last is closed first. If you forget the closing right brace, the compiler may not realize the structure is incomplete until the end of the file, so the arrow position may not be where you originally missed it.
A useful habit: After typing an opening parenthesis, immediately add the closing one, then return to fill in the middle. An editor's formatting feature can expose indentation issues, but the final result still depends on the compiler.
Rule 2: Semicolons Are Only Required for Certain Statements
Statements such as variable declarations, assignments, method calls, and return typically end with an English semicolon:
int count = 1;
count = count + 1;
System.out.println(count);
return;Class declarations, method declarations, and statements followed by a code block of if, for, and while typically do not require a semicolon after the closing brace:
if (count > 0) {
System.out.println("positive");
}Therefore, "every line should have a semicolon" is not a rule. Java allows a single statement to span multiple lines or multiple short statements to be written on the same line; whether or not you break lines primarily affects readability, not the syntax, which is determined by the tokens.
The full-width semicolon ; is different from the English semicolon ;. When full-width punctuation is mistakenly used in source code, compilers typically report illegal characters or syntax errors.
Rule 3: Identifier Case Sensitivity
The following names are distinct:
System system
String string
main Main
HelloWorld Helloworldclass, public, and static are keywords; System, String, main, and println are not keywords; they are respectively class names, method names, or member names. The editor's color scheme is determined by the theme and semantic analysis, and "incorrect color" should not be taken as a reliable diagnostic. Prioritize reading the error types, line numbers, and caret position from javac.
Common naming conventions:
- Class names use PascalCase:
HelloWorld,BankAccount; - Methods and variables use camelCase:
main,accountName; - Constants are typically written in uppercase with underscores:
MAX_RETRY_COUNT.
The conventions are not all enforced by the compiler, but they can reduce reading costs.
Rule 4: Public Top-Level Types and File Names
// File:HelloWorld.java
public class HelloWorld {
}If it is saved in hello.java, the compiler will report that public classes should be located in HelloWorld.java.
Removing public makes HelloWorld a package-private top-level class, not "still public." Java allows file names to differ, but this exception should not be used without a strong justification. For beginner projects, use one file per main class and have the file name match the class name.
Rule 5: Whitespace Separates Tokens; Inside Strings, It Is Data
The following two code snippets have the same semantics, but the second is harder to read:
int total = 1 + 2;int total=1+2;Whitespace inside a string becomes part of the output:
System.out.println("Hello World");
System.out.println("HelloWorld");These two lines produce different results. Do not alter the content of a string for the sake of "neatness."
Minimum Check Order
1. Does the filename match the public class name?
2. Are parentheses and quotes properly paired and nested?
3. Are statements that require a semicolon using English semicolons?
4. Are the capitalization of class names, method names, and variable names consistent?
5. Start fixing from the first compilation error, then recompile.Errors that follow may result from a chain reaction of syntax violations. Addressing the earliest one at a time often resolves multiple error messages simultaneously.
Before Moving On
Explain why Java does not require a semicolon on every line, why System and String are not keywords, and why a missing delimiter may be reported later in the file. When debugging, start with the first diagnostic and fix one clear cause at a time.
Now, look at what these errors actually look like.