Java - Language Fundamentals & Syntax
Language Fundamentals & Syntax
This page explains the building blocks of Java source code: how files are organized, what identifiers and keywords are, how to write comments and blocks, and how naming conventions make code readable at scale.
Source File Structure
Each public class is typically declared in its own .java
file with the same name as the class.
package com.example.hello; // 1) package declaration (optional but recommended)
import java.util.List; // 2) imports (types you reference by simple name)
/**
* 3) Type declarations (classes, interfaces, enums, records)
*/
public class Hello { // filename should be Hello.java
public static void main(String[] args) {
System.out.println("Hello");
}
}
Teacher tip: Reinforce the mapping of package → folders and class name → file name. This prevents mysterious compiler errors for newcomers.
Identifiers
- Names you choose for classes, methods, variables, packages.
- May contain letters, digits,
_
, and$
; must not start with a digit. - Are case-sensitive:
value
andValue
are different.
int count = 0; // ok
int Count = 0; // different identifier
// int 2things = 0; // invalid (starts with digit)
Keywords and Reserved Words
Keywords have special meaning (e.g., class
, public
, if
, return
) and cannot be used as identifiers.
Comments
//
single-line comments/* ... */
multi-line comments/** ... */
Javadoc comments for API docs
/** Adds two numbers. */
public static int add(int a, int b) { // a and b are integers
return a + b; /* returns the sum */
}
Blocks and Scope
Blocks are delimited by { ... }
. Variables declared inside a block are not visible outside that block.
if (true) {
int x = 10; // x lives only inside this block
}
// System.out.println(x); // error: x cannot be resolved here
Common pitfall: Declaring variables in a loop header vs inside the loop body affects their scope and reuse.
Whitespace and Formatting
- Java ignores extra spaces and newlines but conventions matter for readability.
- Standard style: 2 or 4 spaces indentation; braces on the same line as statements.
if (condition) {
doWork();
} else {
fallback();
}
Naming Conventions
- Classes, Interfaces: PascalCase (e.g.,
UserService
) - Methods, Variables: camelCase (e.g.,
findUser
,retryCount
) - Constants: UPPER_SNAKE_CASE (e.g.,
MAX_RETRIES
) withfinal
- Packages: all lowercase dot-separated (e.g.,
com.example.api
)
Unicode and Source Encoding
Modern builds standardize on UTF‑8. Non-ASCII identifiers are technically allowed but avoided in most codebases.
Try It: jshell
Java’s REPL helps experiment quickly:
jshell
jshell> int x = 40 + 2
x ==> 42
jshell> /exit
Summary Checklist
- One public top-level class per file, filename matches class.
- Use packages that match folders.
- Follow naming conventions to aid readability.
Architect note: Enforce a formatter (e.g., Spotless/Google Java Format) in CI to keep diffs clean and onboarding smooth.
Try it
- Write a class with valid/invalid identifiers; fix the invalid ones to compile.
- Add Javadoc to a method, then generate docs with
javadoc
or view in IDE. - Use
jshell
to test a smallif
/else
snippet and aswitch
expression.