Java Operator Details

  • 2021-12-11 17:41:04
  • OfStack

Directory 1, Arithmetic Operator 2, Relational Operator 3, Logical Operator 4, Bit Operator 5, Assignment Operator 6, Conditional Operator 7, instanceof Operator

The operators in Java are basically the same as those in C language.

1. Arithmetic operators

操作符 描述 例子
+ 加法 : 相加运算符两侧的值 A + B 等于 30
- 减法 : 左操作数减去右操作数 A – B 等于 -10
* 乘法 : 相乘操作符两侧的值 A * B 等于200
/ 除法 : 左操作数除以右操作数 B / A 等于2
++ 自增: 操作数的值增加1 B++ 或 ++B 等于 21
-- 自减: 操作数的值减少1 B-- 或 --B 等于 19

2. Relational operators

运算符 描述 例子
== 检查如果两个操作数的值是否相等,如果相等则条件为真。 (A == B)为假。
!= 检查如果两个操作数的值是否相等,如果值不相等则条件为真。 (A != B) 为真。
检查左操作数的值是否大于右操作数的值,如果是那么条件为真。 (A > B)为假。
检查左操作数的值是否小于右操作数的值,如果是那么条件为真。 (A < B)为真。
>= 检查左操作数的值是否大于或等于右操作数的值,如果是那么条件为真。 (A >= B)为假。
<= 检查左操作数的值是否小于或等于右操作数的值,如果是那么条件为真。 (A <= B)为真。

3. Logical operators

操作符 描述 例子
&& 称为逻辑与运算符。当且仅当两个操作数都为真,条件才为真。 (A && B) 为假。
` ` 称为逻辑或操作符。如果任何两个操作数任何1个为真,条件为真。
! 称为逻辑非运算符。用来反转操作数的逻辑状态。如果条件为true,则逻辑非运算符将得到false。 !(A && B) 为真。

4. Bitwise operators

操作符 描述 例子
如果相对应位都是1,则结果为1,否则为0 (A&B),得到12,即0000 1100
` ` 如果相对应位都是0,则结果为0,否则为1
^ 如果相对应位值相同,则结果为0,否则为1 (A ^ B)得到49,即 0011 0001
~ 按位取反运算符翻转操作数的每1位,即0变成1,1变成0。 (〜A)得到-61,即1100 0011
<<  按位左移运算符。左操作数按位左移右操作数指定的位数。 A << 2 得到240,即 1111 0000
>>  按位右移运算符。左操作数按位右移右操作数指定的位数。 A >> 2 得到15即 1111
>>>  按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充。 A>>>2 得到15即0000 1111

5. Assignment Operator

操作符 描述 例子
= 简单的赋值运算符,将右操作数的值赋给左侧操作数 C = A + B 将把A + B得到的值赋给C
+= 加和赋值操作符,它把左操作数和右操作数相加赋值给左操作数 C += A 等价于C = C + A

Similar += Other arithmetic, bitwise, and logical operators can be added to the = Up front.

6. Conditional Operators


variable x = (expression) ? value_if_true : value_if_false;

7. instanceof Operator

This operator is used to manipulate an object instance to check whether the object is of a specific type (class type or interface type).


variable instanceof class_or_interface_type_name;

For example:


String name = "James";
boolean result = name instanceof String; //  Due to  name  Yes  String  Type, so return true 


Related articles: