Pro Java 9 Games Development Leveraging the JavaFX APIs

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

If you wanted to subtract Y from X, you would use a minus sign rather than a plus sign, and if you
wanted to multiply the X and Y values, you would use an asterisk character, rather than a plus sign. If you
wanted to divide X by Y, you would use a forward slash character, instead of using a plus sign. If you wanted
to find the remainder of divide X by Y, you would use a percentage sign character. Here is how these basic
arithmetic operations would look in code:


Z = X - Y; // Subtraction Operator
Z = X * Y; // Multiplication Operator
Z = X / Y; // Division Operator
Z = X % Y; // Modulus Operator


You should be careful if your Java code involves division by zero (0). Dividing an integer by 0 will result
in an ArithmeticException. Dividing a floating-point value by 0 will result in +Infinity, -Infinity, or NaN. In
game development environments, it is possible that you might encounter this scenario, and you will have to
redesign your programming logic to make sure that these scenarios do not interfere with your gameplay.
You will be using these arithmetic operators quite a bit during this book, so you will get some great
practice with these before you’re done with your game! Let’s take a closer look at relational operators next, as
sometimes you will want to compare values rather than calculating values precisely.


Java Relational Operators: Making Comparisons


The Java relational operators can be used to make logical comparisons between two variables or between
a variable and a constant, in some circumstances. These should also be familiar to you from junior high
school, and they include equals, not equal, greater than, less than, greater than or equal to, and less than or
equal to. The greater than uses the open end of the arrow (chevron) since the open span is greater than the
closed span, and the less than uses the closed end of the arrow (chevron) since the closed span is less than
the open span. This is a great way to look at this visually; when you do, you can immediately see that in the
relational operator X > Y, X is (on the) greater than (side of ) Y. In Java, the equal to relational operator uses
two equals signs, side by side, between the data fields being compared and uses an exclamation point
before an equals sign is used to denote not equal, as you can see in Table 5-3, which shows the relational
operators along with an example and a description of each.


Table 5-3. Java Relational Operators, an Example Where A=10 and B=20, and a Description of the Relational
Operation


Operator Example Description


== (A == B) not true Comparison of two operands: if they are equal, then the condition
equates to true


!= (A != B) is true Comparison of two operands: if they are not equal, the condition
equates to true



(A > B) not true Comparison of two operands: if left operand is greater than right
operand, equates to true



< (A < B) is true Comparison of two operands: if left operand is less than right
operand, equates to true



= (A >= B) not true Compare two operands: if left operand is greater or equal to right
operand equates to true



<= (A <= B) is true Compare two operands: if left operand less than or equal to right
operand, equates to true

Free download pdf