Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


Writing Java Syntax: Comments and Code Delimiters


There are a couple of things that you need to understand right off of the bat regarding writing Java syntax.
Syntax controls how Java “parses” things regarding the programming language. Parsing your code syntax
allows Java to understand what it is that you want to do with your programming logic. The primary syntax
rules are important to understand because they allow the Java compiler to understand how you are
structuring Java code. Java compilation is the part of a Java programming process where the JDK compiler
(program) turns your Java code into bytecode. This gets executed (run) by the JRE Java Runtime Engine,
which is installed on the end user’s computer system. This Java compiler needs to know what parts of your
code are Java programming logic and what parts are comments to yourself (or comments to other members
of your project programming team); where your Java code blocks begin and end; and, inside of those Java
code blocks, where your individual Java programming statements or instructions begin and end. Once this is
clear to the compiler, it can parse the statements and turn them from code into bytecode.
Let’s start with comments, as this topic is the easiest to grasp. There are two ways to add comments to
Java code: single-line or in-line comments, which can be placed right after each line of Java code logic, and
multiple-line or block comments, which are placed before (or after) a line of Java code or a block of Java
code (a Java code structure).
A single-line comment is used to add a comment regarding what a line of Java code, or a Java
programming statement, is doing. This comment explains what that line of Java code is there to accomplish
within your overall code structure. Single-line comments in Java will start with a double forward slash
character sequence. For instance, if you want to comment one of your import statements in the BoardGame
bootstrap code that you will be creating later in Chapter 6 , you would add double forward slashes after the
line of code. This is what your line of Java code would look like once it has been single-line commented; it is
also shown in Figure 5-1 at the bottom-right side of NetBeans:


import javafx.stage.Stage; // This line of code imports Stage class from JavaFX.stage
package


Let’s also take a look at multiline comments, shown at the top of Figure 5-1 above the package
invincibagel statement, which we’ll be learning about in the next section of this chapter. As you can see,
block comments are done differently, using a single forward slash next to an asterisk to start the comment,


Figure 5-1. Multiline comments (first five lines of code at the top) and single-line comments (last three lines of
code at the bottom)

Free download pdf