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

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

主站蜘蛛池模板: 泰安市| 观塘区| 庆城县| 淳安县| 永春县| 黄浦区| 东城区| 田东县| 漳州市| 房产| 西畴县| 瑞安市| 自治县| 大庆市| 广州市| 克拉玛依市| 全州县| 平遥县| 乌拉特后旗| 龙陵县| 乌拉特后旗| 开江县| 门头沟区| 福鼎市| 信宜市| 巧家县| 德格县| 曲麻莱县| 五河县| 崇义县| 聊城市| 武城县| 漳州市| 城口县| 改则县| 麟游县| 石屏县| 新安县| 友谊县| 武安市| 卓资县|