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:
- The assignment operator (
=
): 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;
- The addition operator (
+
): 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;
- The subtraction operator (
-
): 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;
- The division operator (
/
): 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;
- The multiplication operator (
*
): This multiplies variables and numbers together. For example:answer = 10 * 10;
biggerAnswer = 10 * answer;
- The increment operator (
++
): This is a neat way to add 1 to something.myVariable ++;
is the same as myVariable = myVariable + 1;
- The decrement operator (
--
): You guessed it. This is a shorthand way to subtract 1 from something.myVariable = myVariable -1;
- is the same as
myVariable --;
Note
The operators are grouped. For example, the division operator is one of the multiplicative operators
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.
Tip
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.
Casting
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
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:
- Declares and initializes a
String
variable calledfirstName
to hold the value"Ralph"
- Declares and initializes a
String
variable calledlastName
to hold the value"Baer"
- Concatenates
firstName
to a space character" "
followed bylastName
The fullName
String
now holds the value "Ralph Baer"
, note the space in the middle of the first and last names.