Changing values in variables with operators

Of course, in almost any program, we are going to need to do things with these values. We manipulate (change) variables with operators. Here is a list of perhaps the most common Java operators 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 being a bit more adventurous:

  • The assignment operator (=): This makes the variable to the left of the operator the same as the value to the right. For example, unreadMessages = newMessages;.
  • The addition operator (+): This adds together values on either side of the operator. It is usually used in conjunction with the assignment operator, or slightly differently; add together two variables that contain numeric values. Perhaps like this unreadMessages = newMessages + unreadMessages; or accountBalance = yesterdaysBalance + todaysDeposits;. Notice it is perfectly acceptable to use the same variable, simultaneously on both sides of an operator.
  • 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, unreadMessages = unreadMessages - 1; or accountBalance = accountBalance - withdrawals;.
  • 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;.
  • The multiplication operator (*): This multiplies variables and numbers together. For example, answer = 10 * 10; or biggerAnswer = 10 * 10 * 10;.
  • The increment operator (++): This is a really neat way to add 1 to something. myVariable = myVariable + 1; is the same as myVariable ++;.
  • The decrement operator (--): You guessed it. This is a really neat way to subtract 1 from something. myVariable = myVariable -1; is the same as myVariable --;.

Note

The formal names for these operators are slightly different to that previously explained. For example, the division operator is actually one of the multiplicative operators, but the names given previously are far more useful for the purpose of learning Java, and if you used the term division operator while conversing with someone from the Java community, they would know exactly what you mean.

There are actually many more operators than this in Java. We will meet a whole bunch more later in this chapter when we learn about decisions in Java.

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 required to complete the projects will be fully explained in this book. The link is provided for the curious among us.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.249.105