APCS – Unit 2 – Lesson7. Syntax vs Style

1. Java Syntax
1.1) Syntax Rules

The syntax rules of a language dictate exactly how the vocabulary elements of the language can be combined to form statements. These rules must be followed in order to create a program. 

1.2) Java Syntax Rules

We have already seen some of the Java syntax rules in the “Hello” program. For instance, curly brackets are used to begin and end classes and methods is a syntax rule. During compilation, all syntax rules are checked. If a program is not syntactically correct, the compiler will issue error messages and will not produce bytecode. Another example is the spelling of the “println()” method. If we misspell “println”, a syntax error will appear.

Not only spelling, but also every punctuation mark and symbol in the program has a precise purpose and omitting or misplacing one symbol leads to an error. In the beginning it is hard to get this rigidity of syntax.

Here are some important tips that you must keep in mind (Source: Litvins, Java Methods):

  • Java syntax is NOT very forgiving and may frustrate a novice. The proper response is to pay closer attention to details!
  • Notwithstanding the compiler’s somewhat limited capacity to pinpoint your syntax errors, you can NEVER blame the compiler for errors! You may be sure that there is something wrong with your code or a component (for an example, a required class) is missing if your program does not compile correctly.
  • Beginners can usually save a lot of time by carefully reading their code a couple of times before running it through the compiler. Get in the habit of checking that nothing is misspelled and that all semicolons, braces and other punctuation marks are where they should be.
2. Java Semantics

The semantics of a statement in a programming language define what will happen when that statement is executed. The semantics of a program are usually very well defined. That is, there is one and only one interpretation for each statement.

Please keep in mind, computers are not like human! English allows multiple valid meanings, for an example, the word “bark” has multiple meanings, and a human child can easily understand it. However, if a programming language instruction could have two different meanings, a computer would not be able to tell which one to follow!

3. Java Style

Style is how you format your code so that it is easy to read and thus easier to understand. Good style includes the use of whitespace, comments, indentation, and naming.

Good style will make your teammates and your teacher happy! 

4. Errors / Bugs

An error is an illegal statement or operation performed by the programmer that causes the application or the program to behave abnormally and restricts the application from performing its tasks efficiently. Errors usually remain undetected until the program is compiled or executed. Some may cause the program’s termination or may inhibit the program from getting compiled or executed.

When can an error occur :

  • Java programming language has a set of rules to write its program. An error can occur if the programmer will not follow these rules and regulations.
  • When a programmer performs an operation that is not intended because of a wrong idea or concept.
List of Citations, References and Sources:
  • Lewis, Loftus & Cocking, Java Software Solutions for AP Computer Science
  • Litvins, Java Methods
  • https://www.cs.utah.edu/~germain/PPS/Topics/programming_style.html
  • http://icarus.cs.weber.edu/~dab/cs1400/chap.02/structure.html
  • Images from the internet