Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


Just as the Java programming language uses the double forward slash and slash-asterisk pairing to
delimit the comments in your Java code, there are a couple other key characters that are used to delimit
Java programming statements, as well as to delimit entire blocks of Java program logic. I often call Java code
blocks code structures.
The semicolon character is utilized in Java (all versions) to delimit or separate Java programming
statements such as the package and import statements shown in Figure 5-1. What the Java compiler does is
look for a Java keyword, which starts a Java statement, and then takes everything after that keyword as being
part of that Java code statement until it reaches the semicolon character, which is the way that you tell the
Java compiler “I am done coding this Java statement.” For instance, to declare your Java package at the top
of your Java application, you would use the Java package keyword, the name of your package, and then a
semicolon character, as follows (as shown in Figure 5-1):


package invincibagel;


We will be covering APIs and packages in the next section, as well as how they are accessed by using
import statements. Import statements are also delimited using the semicolon character (also shown in
Figure 5-1). The import statement starts with the import keyword, the package and class to be imported, and
finally, the semicolon delimiter, as shown in the following Java programming statement:


import javafx.application.Application;


The next delimiters that we should take a look at are the curly braces {...}. Like a multiline comment
delimiter, curly braces feature an opening { curly brace, which delimits (or shows the compiler) the
beginning, or start, of a collection of Java statements, and a closing } curly brace, which delimits (or shows
the compiler) the end of a collection of Java programming statements. Curly braces allow you to nest Java
programming statements inside of other Java constructs. We’ll be covering nesting Java constructs frequently
throughout this book.
As you can see in Figure 5-2, Java code blocks delimited using these curly braces can be nested
(contained) inside of each other, allowing more complex Java code structures. Figure 5-2 shows the first
(outermost) code block using curly braces in your class. Inside of that is your start() method, inside of that
is your .setOnAction() method call, and inside of that is a handle() method definition. We will be taking a
look at what all of this Java code does as this chapter progresses. What I want you to visualize now, which
I am helping you to do by drawing red squares in Figure 5-2, is how these curly brackets are allowing your
methods (and class) to define their own code blocks (structures), each of which are a part of a larger Java
structure, with the largest Java structure being an InvinciBagel class. Each left curly bracket has a matching
right curly bracket, and also notice the indenting of the code so that the innermost Java code structures
are indented the farthest to the right. Each block of Java code is indented by an additional four characters,
or spaces. As you can see, the class is not indented (zero), the start() method is four spaces in, the
.setOnAction() method is eight spaces in, and the handle() method is twelve spaces in. Note that NetBeans
9 will indent each of your Java code structures for you.

Free download pdf