Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

Next, let’s take a look at reference data types, which are termed this because they reference more
complex data structures in memory, such as objects and arrays, both of which contain far more complex
data structures that will either hold complex data and method substructures (objects) or will hold more
extensive lists of data (arrays). I will logically cover Java operators, which “operate” on these Java data
structures, in the section right after data types.


Reference Data Types: Objects and Arrays


Object-Oriented Programming (OOP) languages also have reference data types, which provide a reference
in memory to another structure containing a more complex data structure, such as an object or an array.
These more complex data structures are created using code. In the case of Java, this is a class. There are
Java Array classes of one type or another that create arrays of data (like simple databases), as well as the
constructor method in any Java class, even custom classes that you create, which can create the object
structure in memory, which can contain both Java code (methods) as well as data (fields).
Since a reference data type is a reference to a memory location, the default value is always null, which
will signify that the object has not been created yet, as there is no reference in place. Since there are different
Array and DataSet classes, arrays are also reference objects, but since they are created by class constructor
methods, they are actually objects. The bottom line is that reference data types are created using classes and
are always an object of one type or another, which is referenced in memory. Usually this reference is static
and/or final so that the memory location is fixed and memory use is therefore optimized. Next, let’s take a
look at Java operators that are utilized to operate on (that is, perform operations on or with) the different Java
data types that we have just covered.


Java Operators: Manipulating Data in the Application


In this section we are going to cover some of the most often used operators in the Java programming
language, especially the ones that are the most useful for programming games. These include the arithmetic
operators, used for mathematical expressions; the relational operators, used to ascertain relationships
(equal, not equal, greater than, less than, etc.) between data values; the logical operators, used for boolean


Table 5-1. Primitive Data Types in Java 9 Along with Their Default Values, Size in Memory, Definition, and
Numeric Range


DataType Default Binary Size Definition Range


boolean false 1 bit (or 8 in 1 byte) A true or false value 0 to 1 (false or true)


char \u0000 16 bit A Unicode character \u0000 to \uFFFF


byte 0 8 bit A signed integer value -128 to 127 (256 total values)


short 0 16 bit A signed integer value -32768 to 32767 (65,536 total
values)


int 0 32 bit A signed integer value -2147483648 to 2147483647


long 0 64 bit A signed integer value -9223372036854775808 to
9223372036854775807


float 0.0 32 bit IEEE 754 floating-point value ±1.4E-45 to ±3.4028235E+38


double 0.0 64 bit IEEE 754 floating-point value ±4.9E-324 to
±1.7976931348623157E+308

Free download pdf