Programming Language Rules
Java syntax defines the permitted vocabulary and construction of Java programs.
Why a Computer Needs Strict Rules
A program is a sequence of instructions intended to make a computer perform a task. A human reading an instruction can often guess its general meaning, even when it is written loosely. A computer cannot work this way. The instructions must be written in a programming language, and that language places strict limits on what may be written and how the parts may be arranged.
This article follows one idea through the whole lesson: a program has to be written in an allowed form before anything else can happen to it. Keep that in mind as you read the examples, because the rest of the article shows what the allowed forms are and what happens when a program steps outside them.
What Syntax Means in Java
Java syntax is the collection of rules that defines the permitted vocabulary and construction of Java programs. It makes Java program text recognizable as a program the computer can process.
The word syntax covers two questions. The first is which words and symbols are allowed at all, which is the vocabulary. The second is how those allowed pieces may be put together, which is the construction. Both questions are answered by the syntax rules, and a program must satisfy both of them.
Vocabulary and Construction
The clearest way to reason about syntax is to check two things separately: the individual language elements, and the way they are arranged. Vocabulary supplies the permitted building pieces. Construction rules determine how those pieces can form instructions and larger structures.
Tracing a Program Through the Syntax Check
Follow a program from the moment it is written. The first gate it reaches is the syntax check. Only a program that passes this gate can go on to be successfully compiled or interpreted. The diagram below shows the two paths a program can take.
The diagram makes the key point visible. Syntax is an entry condition, not a later processing step. A program that breaks a rule is rejected at the gate, and compilation or interpretation never gets a chance to succeed for it. Notice also that the program is rejected rather than accepted as a syntactically correct program. An error message may help you fix the problem, but the essential consequence is the rejection itself.
What do you think happens?
A Java statement is missing its closing semicolon. Will the program move on to be successfully compiled?
Reveal answer
No, because the program breaks a syntax rule and is rejected
A syntax error is not merely a style issue. It means the program has crossed outside the forms the language allows, so it is rejected rather than accepted as syntactically correct.
Anatomy of a Single Statement
To see vocabulary and construction working together, take one small statement and examine each part. The statement below is a generated example written for this article. It declares a variable named score and gives it the value 10.
- int is a keyword from the permitted vocabulary
- score is an identifier chosen by the programmer
- = is the assignment operator
- 10 is a literal value
- ; ends the statement
Each part is accepted because it occupies a position the construction rules allow. The keyword int is part of the permitted vocabulary. The identifier score is a name the programmer chooses, placed where a name is allowed. The literal 10 and the semicolon are arranged in the order the rules require. Change any of these pieces to something the vocabulary does not allow, or move one out of its allowed position, and the statement stops being syntactically correct.
One Character Can Change the Outcome
Syntax rules are strict enough that a single missing character can decide whether a program is accepted. The before and after versions below are generated for this article. The only difference is the missing semicolon at the end.
int score = 10;Follows the syntax rules and can move on to compilation
int score = 10Breaks the syntax rules and is rejected before use
Checking Rules Before Reasoning About the Task
When a program is rejected, the source recommends a specific habit. Begin with the language rules before reasoning about what the program was meant to do. The steps below are a worked procedure built from that habit.
Diagnosing a Rejected Program
A program has been rejected before use. Decide what to check first.
- 1Locate the broken syntax rule
If the program is rejected because its form is not permitted, first find the rule it breaks. Check the vocabulary for disallowed pieces, then check the arrangement of those pieces.
- 2Repair the form
Correct the syntax problem so the program becomes syntactically acceptable. For example, restore a missing semicolon or fix a misspelled keyword.
- 3Reason about the task
Only after the program is syntactically acceptable does it make sense to consider whether it performs the task it was meant to perform.
The syntax rules are satisfied first, so the program can be compiled or interpreted and its behavior can be judged.
Three generated lines of Java are shown below, each on its own. For each line, decide whether it breaks a syntax rule and would be rejected before use. Line one: int count = 3; Line two: int count = 3 Line three: int 5count = 3; Explain your decision for each line using the vocabulary and construction ideas from this article.
Hints
- Ask first whether every piece is part of the permitted vocabulary.
- Then ask whether the pieces appear in an arrangement the construction rules allow.
- A missing semicolon is a syntax error, not a style choice.
Mistakes Beginners Make with Syntax
Common mistake Assuming a human can understand the intent, so the program should run
What it looks likeA line that is clearly meant to set a score is written with a missing semicolon.
Why it failsThe computer cannot use instructions simply because a human can recognize their general meaning. The program must first satisfy the syntax rules.
Do this insteadCheck the form against the language rules before judging whether the intent is right.
Common mistake Treating a syntax error as a style issue
What it looks likeDeciding that a missing semicolon is a minor formatting choice and ignoring it.
Why it failsA syntax error means the program has crossed outside the forms allowed by the language, and the program is rejected.
Do this insteadTreat every syntax error as a rejection that must be fixed before compilation or interpretation can succeed.
Common mistake Reasoning about the task before fixing the form
What it looks likeSpending time debugging the logic of a program that the language has already rejected.
Why it failsSyntax is an entry condition. Logic cannot be evaluated until the program is syntactically acceptable.
Do this insteadLocate the broken syntax rule first, then consider the program's intended task.
Common mistake Believing that a rejection message is the whole problem rather than a hint about the broken rule
Key Takeaways
- Java syntax defines the permitted vocabulary, which supplies the building pieces, and the construction rules, which determine how those pieces may be arranged into instructions and larger structures.
- A program must follow syntax rules before it can be successfully compiled or interpreted, so syntax acts as an entry condition.
- A program that breaks a syntax rule is rejected rather than accepted, and the rejection itself is the essential consequence even when an error message is shown.
- When a program is rejected, first locate the broken syntax rule and fix the form, and only then reason about whether the program performs its intended task.