Of course, in almost any game, we are going to need to do things with these values. We manipulate variables with operators.
As we did with variable types, let's discuss the most used operators in this book.
Here is a list of the most common Java operators we will use in this book that allow us to manipulate variables. You do not need to memorize them as we will look at every line of code as and when we use them for the first time. We have already seen the first operator when we initialized our variables =
, but we will see it again while it is being a bit more adventurous.
Once you have seen one operator you can guess what the others do but some examples of each will help to get familiar with them:
=
): This makes the variable to the left of the operator the same as the value or variable to the right. For example: score = 1000;
highScore = score;
+
): This adds together values on either side of the operator. It is usually used in conjunction with the assignment operator to add together two values or variables that have numeric values. Notice it is perfectly acceptable to use the same variable, simultaneously on both sides of an operator. Perhaps like this: horizontalPosition = horizontalPosition + movementSpeed;
score = score + 100;
-
): This subtracts the value on the right side of the operator from the value on the left. Usually used in conjunction with the assignment operator. Perhaps like this: numberAliens = numberAliens - 1;
timeDifference = currentTime - fastestTime;
/
): This divides the number on the left by the number on the right. Again, usually used in conjunction with the assignment operator. For example: fairShare = numSweets / numChildren;
framesPerSecond = numSeconds / numFrames;
*
): This multiplies variables and numbers together. For example:answer = 10 * 10;
biggerAnswer = 10 * answer;
++
): This is a neat way to add 1 to something. myVariable ++;
is the same as myVariable = myVariable + 1;
--
): You guessed it. This is a shorthand way to subtract 1 from something. myVariable = myVariable -1;
myVariable --;
There are more operators than this in Java. We will meet a whole bunch later in Chapter 7, Making decisions with Java If, Else and Switch.
If you are curious about operators, there is a complete list of them on the Java website here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html. All the operators needed to complete the projects will be fully explained in this book. The link is provided for the extra curious readers.
A note about declarations, assignments, and operators; when we bundle these elements together into some meaningful syntax, we call it an expression.
Let's have a look at a couple of special cases of using operators with variables.
Sometimes we have one type of variable, but we absolutely must have another type. As we have seen, if we use the wrong type with a variable then the compiler will give us an error. One solution is to cast a value or variable to another type. Look at this code.
float a = 1.0f int b = (int) a;
The b
variable now equals 1. Be aware that if a
had held some fractional value after the decimal point that part would not be copied to b
. However, this is still sometimes useful, and the full float
value stored in a
remains unchanged.
Note that not all values can be cast to all types but float
to int
and int
to float
is quite common. We will use casting later in this project.
Concatenation sounds complicated, but it is straightforward, and we have already seen it we just haven't talked about it. Concatenation is where we join or add String
values together. Have a look at this code
String firstName = "Ralph"; String lastName = "Baer" String fullName = firstName + " " + lastName
The previous code does three things:
String
variable called firstName
to hold the value "Ralph"
String
variable called lastName
to hold the value "Baer"
firstName
to a space character " "
followed by lastName
The fullName
String
now holds the value "Ralph Baer"
, note the space in the middle of the first and last names.
3.144.9.169