One of the most basic uses of a computer is to perform mathematical operations. As a computer language, Java also provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:
Arithmetic operators
Relational operators
Bitwise operators
Logical operators
Assignment operators
Other operators
Arithmetic operators are used in mathematical expressions and their functions are the same as in mathematics. The following table lists all arithmetic operators.
The example in the table assumes that the value of integer variable A is 10 and the value of variable B is 20:
Operator | Description | Example |
---|---|---|
+ | Addition - Phase The value on both sides of the addition operator | A + B is equal to 30 |
- | Subtraction - left operand minus right operand | A – B is equal to -10 |
* | Multiplication - the values on both sides of the multiplication operator | A * B is equal to 200 |
/ | Division - left operand divided by right operand | B/A equals 2 |
% | Modulo - the remainder of the left operand divided by the right operand | B%A is equal to 0 |
++ | Auto-increment: The value of the operand increases by 1 | B++ or ++B is equal to 21 (see the difference below) |
-- | Decrement: the value of the operand is reduced by 1 | B-- or --B is equal to 19 (see the difference below) |
Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
public class Test { public static void main(String args[]) { int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("a-- = " + (a--) ); // 查看 d++ 与 ++d 的不同 System.out.println("d++ = " + (d++) ); System.out.println("++d = " + (++d) ); } }
1. Auto-increment (++) and self-decrement (--) operators Is a special arithmetic operator that requires two operands to perform operations, while the increment and decrement operator requires one operand.
public class selfAddMinus{ public static void main(String[] args){ int a = 3;//定义一个变量; int b = ++a;//自增运算 int c = 3; int d = --c;//自减运算 System.out.println("进行自增运算后的值等于"+b); System.out.println("进行自减运算后的值等于"+d); } }
The running result is:
进行自增运算后的值等于4进行自减运算后的值等于2
Analysis:
int b = ++a; 拆分运算过程为: a=a+1=4; b=a=4, 最后结果为b=4,a=4 int d = --c; 拆分运算过程为: c=c-1=2; d=c=2, 最后结果为d=2,c=2
2. Prefix auto-increment and auto-subtraction method (++a,--a): First perform the self-increment or self-decrement operation, and then perform the expression operation.
3. Suffix auto-increment and auto-subtraction (a++,a--): First perform expression operation, then perform auto-increment or Example of auto-decrement operation:
public class selfAddMinus{ public static void main(String[] args){ int a = 5;//定义一个变量; int b = 5; int x = 2*++a; int y = 2*b++; System.out.println("自增运算符前缀运算后a="+a+",x="+x); System.out.println("自增运算符后缀运算后b="+b+",y="+y); } }
The operation result is:
自增运算符前缀运算后a=6,x=12自增运算符后缀运算后b=6,y=10
The following table shows the relational operators supported by Java
In the example in the table, the value of integer variable A is 10 and the value of variable B is 20:
Operator | Description | Example |
---|---|---|
== | Check if Whether the values of the two operands are equal, if so, the condition is true. | (A == B) is false (not true). |
!= | Checks if the values of the two operands are equal, if the values are not equal then the condition is true. | (A != B) is true. |
> | Check whether the value of the left operand is greater than the value of the right operand, if so then the condition is true. | (A>B) is not true. |
4d7c389e6169f0369b7dfd7861ef9159 = | Checks whether the value of the left operand is greater than or equal to the value of the right operand, if so then the condition is true. | (A> = B) is false. |
c7ff83374ed54468df39e71acb823335> | Bitwise right shift operator. The left operand is shifted bitwise right by the number of bits specified by the right operand. | A >> 2 gets 15, which is 1111 |
Bitwise right shift zero-fill operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the resulting vacancies are filled with zeros. | A>>>2 gets 15 which is 0000 1111 |
Operator | Description | Example |
---|---|---|
&& | is called logic AND operator. A condition is true if and only if both operands are true. | (A && B) is false. |
| | | is called the logical OR operator. The condition is true if either of the two operands is true. | (A | | B) is true. |
! | is called the logical NOT operator. Used to invert the logical state of the operand. If the condition is true, the logical NOT operator will get false. | ! (A && B) is true. |
复制并粘贴下面的Java程序并保存为TestA.java文件,然后编译并运行这个程序:
public class TestA { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a || b = " + (a||b) ); System.out.println("!(a && b) = " + !(a && b)); } }
以上实例编译运行结果如下:
a && b = falsea || b = true!(a && b) = true
当使用与逻辑运算符时,在两个操作数都为true时,结果才为true,但是当得到第一个操作为false时,其结果就必定是false,这时候就不会再判断第二个操作了。
public class LuoJi{ public static void main(String[] args){ int a = 5;//定义一个变量; boolean b = (a<4)&&(a++<10); System.out.println("使用短路逻辑运算符的结果为"+b); System.out.println("a的结果为"+a); } }
运行结果为:
使用短路逻辑运算符的结果为falsea的结果为5
解析: 该程序使用到了短路逻辑运算符(&&),首先判断 a2975f76be6eb576811ac493ee5424d7f> =
Right shift assignment operator C >> = 2 is equivalent to C = C >> 2 ##& = Bitwise AND assignment operator C&= 2 is equivalent to C = C&2 ^ = Bitwise XOR assignment operator C ^ = 2 is equivalent to C = C ^ 2 | = Bitwise OR assignment operator C | = 2 is equivalent to C = C | 2
复制并粘贴下面的Java程序并保存为TestA.java文件,然后编译并运行这个程序:
public class TestA { public static void main(String args[]) { int a = 10; int b = 20; int c = 0; c = a + b; System.out.println("c = a + b = " + c ); c += a ; System.out.println("c += a = " + c ); c -= a ; System.out.println("c -= a = " + c ); c *= a ; System.out.println("c *= a = " + c ); a = 10; c = 15; c /= a ; System.out.println("c /= a = " + c ); a = 10; c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <<= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c ); c >>= 2 ; System.out.println("c >>= a = " + c ); c &= a ; System.out.println("c &= 2 = " + c ); c ^= a ; System.out.println("c ^= a = " + c ); c |= a ; System.out.println("c |= a = " + c ); } }
以上实例编译运行结果如下:
c = a + b = 30c += a = 40c -= a = 30c *= a = 300c /= a = 1c %= a = 5c <<= 2 = 20c >>= 2 = 5c >>= 2 = 1c &= a = 0c ^= a = 10c |= a = 10
条件运算符也被称为三元运算符。该运算符有3个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。
variable x = (expression) ? value if true : value if false
public class Test { public static void main(String args[]){ int a , b; a = 10; // 如果 a 等于 1 成立,则设置 b 为 20,否则为 30 b = (a == 1) ? 20 : 30; System.out.println( "Value of b is : " + b ); // 如果 a 等于 10 成立,则设置 b 为 20,否则为 30 b = (a == 10) ? 20 : 30; System.out.println( "Value of b is : " + b ); } }
以上实例编译运行结果如下:
Value of b is : 30Value of b is : 20
该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)。
instanceof运算符使用格式如下:
( Object reference variable ) instanceof (class/interface type)
如果运算符左侧变量所指的对象,是操作符右侧类或接口(class/interface)的一个对象,那么结果为真。
下面是一个例子:
String name = "James";boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真
如果被比较的对象兼容于右侧类型,该运算符仍然返回true。
看下面的例子:
class Vehicle {} public class Car extends Vehicle { public static void main(String args[]){ Vehicle a = new Car(); boolean result = a instanceof Car; System.out.println( result); } }
以上实例编译运行结果如下:
true
当多个运算符出现在一个表达式中,谁先谁后呢?这就涉及到运算符的优先级别的问题。在一个多运算符的表达式中,运算符优先级不同会导致最后得出的结果差别甚大。
例如,(1+3)+(3+2)*2,这个表达式如果按加号最优先计算,答案就是 18,如果按照乘号最优先,答案则是 14。
再如,x = 7 + 3 * 2;这里x得到13,而不是20,因为乘法运算符比加法运算符有较高的优先级,所以先计算3 * 2得到6,然后再加7。
下表中具有最高优先级的运算符在的表的最上面,最低优先级的在表的底部。
Category | Operator | Associativity | |
---|---|---|---|
Suffix | () [] . (dot operator) | left to right | |
一元 | + + -!~ | Right to left | |
Multiplicative | * /% | Left to right | |
Additive | + - | Left to right | |
Shift | >> > >> efb13194719992a95c8dfae5305d21b5> = 113a2d54e84e0e4ce0f97b0e637e4345> = << =&= ^ = | = | right to left | |
, | left to right |