Arithmetic operators include +, -, *, /, %, ++, --, and are used in the same way as in mathematics.
Suppose the value of variable a is 10 and the value of variable b is 5:
Notice:
1) The operand of an arithmetic operator must be an integer or floating-point variable.
2) Expressions that comply with Java grammar rules and are connected with arithmetic operators and parentheses are called arithmetic expressions, for example: a+2*b-3/(c%d).
3) a++ or a-- means to increase or decrease the value of a by 1 before using a; ++a or--a means to increase or decrease the value of a by 1 after using a.
For example: the value of a is 10, the value of b=a++,b is 10, and the value of b=++a,b is 11.
4) Java has expanded the addition operator to enable string concatenation. For example: "abc" + "def" will get the string "abcdef".
Example:
publicclassMain{publicstaticvoidmain(String[]args){inta=5;intb=10;intc=15;intd=20;System.out.println(a+b=+(a+b));System.out.println( ab=+(ab));System.out.println(a*b=+(a*b));System.out.println(b/a=+(b/a));System.out.println( b%a=+(b%a));System.out.println(a++=+(a++));System.out.println(a--=+(a--));System.out.println( d++=+(d++));System.out.println(++d=+(++d));}}
The running results are as follows:
a+b=15a-b=-5a*b=50b/a=2b%a=0a++=5a--=6d++=20++d=22