- 雙語版Java程序設(shè)計(jì)
- 何月順主編
- 571字
- 2018-12-27 20:14:15
4.1 Arithmetic Operators
Arithmetic operators are used in mathematical expression(數(shù)學(xué)表達(dá)式)in the same way that they are used in algebra(代數(shù)學(xué)). The Table 4.1 lists the arithmetic operators:
Table 4.1 Arithmetic Operators
The operands of the arithmetic operators must be of numeric type. You cannot use them on boolean types, but you can use them on char types, since the char type in Java is, essentially, a subset of int.
class example1 { public static void main(String args[]) { System.out.println("Integer Arithmetic"); int a = 1 + 1; System.out.println("a="+a); } }
when you run this program you will see the following output:
Integer Arithmetic
a = 2
4.1.1 The Modulus Operators
The modulus operator, %, return the remainder of a division operation. It can be applied to floating-point types as well as integer types. The following program demonstrates.
求余運(yùn)算符%,返回除法操作的余數(shù)。它可以應(yīng)用于浮點(diǎn)型,也可以應(yīng)用于整型。下面將給出示例。
class example2 { public static void main(String args[]) { int x = 42; double y = 42.3; System.out.println("x mod 10 = " + x % 10); System.out.println("y mod 10 = " + y % 10); } }
when you run this program you will get following output:
x mod 10 = 2
y mod 10 = 2.299999999999997
4.1.2 Arithmetic Assignment Operators
Java provides special operators that can be used to combine(結(jié)合) an arithmetic operation with an assignment. As you probably know, statements like the following are quite common in programming.
a=a+4;
In Java, you can write this statement as shown here :
a+=4;
This version uses the += assignment operator. Both statements performs the same action: they increase the value of a by 4;here are some more examples.
a=a%2; can be written as a%=2; b=b*3; can be written as b*=3; c=c/5; can be written as c/=5; d=d-7; can be written as d-=7;
4.1.3 Increment and Decrement
The ++ and the - are Java’s increment and decrement operators. The increment operator increases its operand by one. The decrement operator decreases its operand by one.
For example, this statement x=x+1; can be rewritten like this by use of increment operator x++;
Similarly, this statement x=x-1; is equivalent to x--;
These operators are unique in that they can appear both in postfix form, where they follow the operand as just shown, and prefix form, where they precede the operand.
當(dāng)這些運(yùn)算符跟在操作數(shù)的后面時(shí),以后綴形式出現(xiàn);當(dāng)這些運(yùn)算符放在操作數(shù)的前面時(shí),以前綴形式出現(xiàn)。
class example3 { public static void main(String args[]) { int a = 23; int b = 5; System.out.println("a & b : " + a + " " + b); a += 30; b *= 5; System.out.println("after arithmetic assignment a & b: "+a+" "+b); a++; b--; System.out.println("after increment & decrement a & b: "+a+" "+b); } }
when you run this program you will get following output:
a & b : 23 5
after arithmetic assignment a & b : 53 25
after increment & decrement a & b : 54 24
- JMAG電機(jī)電磁仿真分析與實(shí)例解析
- JSF2和RichFaces4使用指南
- Visual C++編程全能詞典
- 控制系統(tǒng)計(jì)算機(jī)仿真
- INSTANT Drools Starter
- 網(wǎng)絡(luò)安全與防護(hù)
- Grome Terrain Modeling with Ogre3D,UDK,and Unity3D
- 電腦上網(wǎng)輕松入門
- SMS 2003部署與操作深入指南
- 三菱FX/Q系列PLC工程實(shí)例詳解
- 電腦上網(wǎng)入門
- Drupal高手建站技術(shù)手冊(cè)
- 與人共融機(jī)器人的關(guān)節(jié)力矩測量技術(shù)
- Raspberry Pi 3 Projects for Java Programmers
- 探索中國物聯(lián)網(wǎng)之路