Java syntax expresses typed declarations, control flow and member access; a reference is useful only when its short forms retain the surrounding validity rules.
Java syntax quick reference
Common patterns
Use the reference
Apply these patterns in the array-sum exercise. The table is a starting point, not a complete language reference.
Declarations do not remove ownership rules
A primitive local stores a value, while a reference local identifies an object or holds null. final prevents reassignment of that local or field after its allowed initialization; it does not recursively freeze the referenced object. final List declarations can still permit membership edits.
A method signature states parameter types and a return type. Overloads share a name with different parameter lists, while an override supplies another implementation of an inherited instance method. Return type alone does not create an overload. See compile-time selection when a short signature list leaves ambiguity.
An interface declaration specifies a type contract. Choosing it for a variable allows different implementations, but their operation costs can still differ. List.get on ArrayList and LinkedList shares a signature without sharing the same indexed-access cost.
Keep failure-sensitive forms nearby
An array index must be at least zero and smaller than length. A loop using less-than-or-equal reaches a forbidden final position. An enhanced-for loop avoids explicit indexing but does not permit arbitrary structural modification of its backing collection.
try-with-resources closes owned AutoCloseable values on scope exit. A catch clause handles specified failures; it should not silently convert all failures into an ordinary success result. A finally block is appropriate for releasing an acquired lock, as the lock example shows.
Version matters. Records require a newer source level than Java 8, and the pattern switch lessons in this curriculum declare Java 21. Do not paste their syntax into a Java 8 build and diagnose the parser error as a missing import. Check the page’s release label before compiling.
Common Mistakes
Check empty inputs and invalid values before applying this operation to application data. State whether a method edits shared state or returns a separate value.
