Lesson · Java Language Fundamentals
Syntax: how Java reads your file
12 min readSyntax
Java's grammar in one pass. A file is a tree of declarations, semicolons end statements rather than lines, braces are the real structure, three comment forms exist, and identifiers obey rules the JDK itself never breaks.
A file is a tree, not a script
A Java file is not executed top to bottom the way a shell script is. It *declares* things. A package, then imports, then types; inside a type, members (fields, methods, nested types); inside a method, statements. The compiler builds this tree first and reasons about it before producing anything.
What looks like layout is actually structure. Braces open and close blocks, and everything between a pair of braces belongs to the declaration in front of it. Indentation is for humans. The braces are for the compiler.
Anatomy of a Java file
click a marked lineClass declaration
An access modifier, the class keyword, a type name in PascalCase, then a block opened with {. A public class must share its file name, which is how the class loader finds it by name before reading anything else.
Statements, expressions, and the semicolon
A statement is an instruction that does something, and it ends with a semicolon. The semicolon ends the statement, not the line, so one statement may span several lines, and a line may contain almost nothing else.
An expression is different: it produces a value. 2 + 3 is an expression. count++ is an expression. A method call is an expression. Expressions live inside statements, which is how int remaining = DAYS - done; reads: a statement wrapping an expression.
- Declaration:
int count = 0;brings a name into scope with a type. - Expression statement:
total = price * qty;computes a value and stores or discards it. - Control flow:
if,for,while,switchgovern blocks and decide what runs. - Block:
{ }groups statements and creates a scope, even with no keyword in front of it.
Three comment forms, one of them documentation
// runs to the end of the line. /* ... */ spans anything it covers, and it does not nest: the first */ closes it. /** ... */ is Javadoc, the same syntax with a different purpose: tooling extracts it into HTML, and IDEs render it on hover.
Javadoc is a contract surface rather than decoration. @param, @return and @throws are read by the javadoc tool, by IDEs, and by the static analysis people run on your code later. Writing it for public API is a habit worth forming from the first file.
Identifiers, keywords, and naming conventions
An identifier is any name you invent. It must start with a letter, $ or _ (letters in practice), may continue with digits, is case sensitive, and has no length limit. A lone _ is reserved, as are roughly fifty keywords such as class, int and new, plus the literals true, false and null. Those last three are reserved despite not being keywords, so they can never be your variable names.
A newer category is contextual: var, yield, record, sealed, permits, when. These are reserved only where the grammar needs them. record is a perfectly good method name and a forbidden class name. yield behaves like an ordinary identifier until it appears inside a switch expression, where it means 'produce this value'. Knowing the difference explains error messages that seem contradictory.
- Types PascalCase:
ArrayList,HttpClient. - Methods and variables camelCase:
parseInt,remainingDays. - Constants (static final) SCREAMING_SNAKE_CASE:
MAX_VALUE,DAYS_IN_WEEK. - Packages lowercase, reverse domain:
java.util.concurrent,org.algolab.hero. - Type parameters single capitals:
T,E,K,V,R.
Block comments do not nest
/* ... */ ends at the first */. Comment out a method that already contains a block comment or Javadoc and the leftover text becomes broken code, often reported far from where you are looking. Use // when disabling code, or let the IDE comment the whole region line by line.
Interview angles
What is the difference between a statement and an expression?+
An expression produces a value (2 + 3, count++, a method call). A statement performs an action and ends with a semicolon; it may wrap an expression. Statements are the sentences of the language; expressions are the noun phrases inside them.
Which characters can start an identifier, and is Java case sensitive?+
A letter, $ or _ (letters in actual use); digits may only appear after the first character. Java is fully case sensitive, so money and Money are different names. The compiler enforces the rules, not the conventions.
Are true, false and null keywords?+
They are reserved literals rather than keywords, but the practical effect is identical: you cannot use them as identifiers. The keyword list plus these three covers every name the language keeps for itself.
What does 'contextual keyword' mean, with an example?+
An identifier that becomes a keyword only in specific grammar positions. record is forbidden as a class name but fine as a method name; yield is an ordinary name until it appears inside a switch expression, where it means 'produce this value'.