官术网_书友最值得收藏!

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

主站蜘蛛池模板: 申扎县| 平度市| 丹江口市| 汾西县| 郴州市| 柳江县| 高密市| 巴南区| 淄博市| 玛纳斯县| 阜康市| 元氏县| 丁青县| 江安县| 视频| 合阳县| 江陵县| 庄河市| 垫江县| 武城县| 赣州市| 四川省| 临桂县| 横峰县| 天全县| 公主岭市| 金门县| 孝感市| 东兰县| 湘阴县| 仲巴县| 海门市| 博乐市| 合山市| 福清市| 石河子市| 长泰县| 尼玛县| 耒阳市| 从江县| 准格尔旗|