28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

A class that has been declared using the abstract modifier keyword cannot be instanced, and it is intended to be<br />

used as a superclass (blueprint) to create (extend) other classes. Because a final class cannot be extended, you will not<br />

use the final and abstract modifier keywords together at the class level. If a class contains any methods that have been<br />

declared using the abstract modifier keyword, the class must itself be declared an abstract class. An abstract class does<br />

not have to contain any abstract methods, however.<br />

A method that has been declared using the abstract modifier keyword is a method that has been declared for<br />

use in subclasses but that has no current implementation. This means that it will have no <strong>Java</strong> code inside its method<br />

body, which, as you know, is delineated in <strong>Java</strong> by using curly braces. Any subclass that extends an abstract class<br />

must implement all these abstract methods, unless the subclass is also declared abstract, in which case the abstract<br />

methods are passed down to the next subclass level.<br />

<strong>Java</strong>’s Volatile Modifier: Advanced Multithreading Control over Data Fields<br />

The <strong>Java</strong> volatile modifier keyword is used when you are developing multithreaded applications, which you are not<br />

going to be doing in basic game development, as you want to optimize your game well enough so that it only uses one<br />

thread. The volatile modifier tells the <strong>Java</strong> virtual machine (JVM), which is running your application, to merge the<br />

private (that thread’s) copy of the data field (variable or constant) that has been declared volatile with the master copy<br />

of that variable in system memory.<br />

This is similar to the static modifier keyword, the difference being that a static variable (data field) is shared by<br />

more than one object instance, whereas a volatile data field (variable or constant) is shared by more than one thread.<br />

<strong>Java</strong>’s Synchronized Modifier: Advanced Multithreading Control over Methods<br />

The <strong>Java</strong> synchronized modifier keyword is also used when you are developing multithreaded applications, which<br />

you are not going to be doing for your basic game development here. The synchronized modifier tells the JVM, which<br />

is running your application, that the method that has been declared synchronized can be accessed by only one thread<br />

at a time. This concept is similar to that of synchronized database access, which prevents record access collisions.<br />

A synchronized modifier keyword likewise prevents collisions between threads accessing your method (in system<br />

memory) by serializing the access to one at a time so that parallel (simultaneous) access to a method in memory by<br />

multiple threads will never occur.<br />

Now that you have studied primary <strong>Java</strong> constructs (classes, methods, and fields) and basic modifier keywords<br />

(public, private, protected, static, final, abstract, and so on), let’s journey inside the curly braces now, learning about<br />

the tools that are used to create the <strong>Java</strong> programming logic that will eventually define your game app’s game play.<br />

<strong>Java</strong> Data Types: Defining Data Type in Applications<br />

Because you have already learned about variables and constants encountered in a few of <strong>Java</strong>’s data types, let’s explore<br />

these next, as it is not too advanced for your current progression from easy to more difficult topics!<br />

There are two primary data type classifications in <strong>Java</strong>: primitive data types, which are the ones that you are the<br />

most familiar with if you have used a different programming language, and reference (object) data types, which you<br />

will know about if you have used another OOP language, such as Lisp, Python, Objective-C, C++, or C# (C Sharp).<br />

Primitive Data Types: Characters, Numbers, and Boolean (Flags)<br />

There are eight primitive data types in the <strong>Java</strong> programming language, as shown in Table 3-1. You will be using<br />

these as you work your way through the book to create your InvinciBagel game, so I am not going to go into detail<br />

regarding each one of them now, except to say that <strong>Java</strong> boolean data variables are used for flags or switches (on/off),<br />

char is used for Unicode characters or to create String objects (an array of char), and the rest are used to hold numeric<br />

60<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!