- Android Programming for Beginners
- John Horton
- 474字
- 2021-07-23 14:47:07
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;
oraccountBalance = 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;
oraccountBalance = 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;
orbiggerAnswer = 10 * 10 * 10;
. - The increment operator (++): This is a really neat way to add
1
to something.myVariable = myVariable + 1;
is the same asmyVariable ++;
. - The decrement operator (--): You guessed it. This is a really neat way to subtract
1
from something.myVariable = myVariable -1;
is the same asmyVariable --;
.
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.
- Python快樂編程:人工智能深度學(xué)習(xí)基礎(chǔ)
- Delphi程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- 程序員面試算法寶典
- Mastering Kotlin
- 征服RIA
- PHP 7+MySQL 8動(dòng)態(tài)網(wǎng)站開發(fā)從入門到精通(視頻教學(xué)版)
- Getting Started with Laravel 4
- 從Java到Web程序設(shè)計(jì)教程
- C++從入門到精通(第5版)
- TMS320LF240x芯片原理、設(shè)計(jì)及應(yīng)用
- QGIS Python Programming Cookbook(Second Edition)
- Getting Started with Python and Raspberry Pi
- IBM Cognos TM1 Developer's Certification guide
- Unity Android Game Development by Example Beginner's Guide
- Java編程指南:語法基礎(chǔ)、面向?qū)ο蟆⒑瘮?shù)式編程與項(xiàng)目實(shí)戰(zhàn)