Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


accessing your method (in system memory) by “serializing” the access to one at a time so simultaneous
access (collision) of a method in memory by multiple threads can never occur. The synchronized keyword is
associated with the properties of visibility and mutual exclusion for the running app. Many multithreading
scenarios do not require mutual exclusion, only visibility, and therefore using a synchronized keyword
instead of a volatile keyword in those situations would be considered overkill (the opposite of optimization).
Now that we have covered primary Java constructs (classes, methods, and fields) and basic modifier
(public, private, protected, static, final, abstract, etc.) keywords, let’s journey inside of the curly braces: { }
now, learning about the tools that are used to create the Java programming logic that will eventually define
your pro Java 9 gameplay.


Java Data Types: Defining Data Types in Applications


Since we have already covered variables and constants, you have encountered a few of the Java data types
already. Let’s get into that topic next, as it’s not too advanced for our current progression from easy-to-
comprehend to more difficult topics! There are two primary data type classifications in Java: primitive
data types, which are the ones that you are probably the most familiar with if you have used different
programming languages, and reference (object) data types, which you are probably familiar with if you
have used another Object-Oriented Programming language, such as LISP, Python, Objective-C, Ruby,
Groovy, Modula, Object COBOL, ColdFusion, C++, and C# (C Sharp and .NET).


Primitive Data Types: Character, Numbers, and Boolean


There are eight primitive data types in the Java programming language, as shown in Table 5-1. We will be
using these during the book to create our JavaFXGame i3D Java 9 game, so I am not going to go into a high
level of detail regarding each one of these now, except to say that boolean data is usually used in games
to hold “flags” or “switches” (on/off ), char data is usually used to contain Unicode characters or is used to
create more complex String objects (which are essentially are an array of char), and the rest are used to hold
numeric values of different sizes and resolutions. Integer values hold whole numbers, while a floating-point
value holds fractional (decimal point value) numbers.
It’s important to use the right numeric data type for a variable’s “scope” or range of use, because as you
can see in Table 5-1, large numeric data types can use up to eight times more memory than the smaller ones.
Notice that a Boolean data value can be 64 times smaller than a long or double numeric value, so designing
your Java 9 games to utilize lots of Boolean values can be an incredible memory optimization technique.
Don’t use any more numeric value resolution than you absolutely need to accomplish your game processing
objective, as memory is a valuable resource.

Free download pdf