Last update:2018-05-08
This is the second section of part 1.
The definition of operators
Operator is a kind of signs which has certain function,in order to notify Java to do certain operations.
There are several common operators down below that you can see in Java:
Arithmetic operators
Arithmetic operators mainly are use to do basic arithmetic operations,such as addition,subtraction, mutiplication and division etc.
Common arithmetic operators in Java:
| Arithmetic operators | Definition | Example |
|---|---|---|
| + | Addition | 5 + 12 = 17 |
| - | Subtraction | 25 - 10 = 15 |
| * | Multiplication | 3 * 8 = 24 |
| / | Division | 24 / 6 = 4 |
| % | Modulus | 24 % 7 = 3 |
| ++ | Increment | int i = 5;i++ |
| – | decrement | int i = 5;i– |
Notice:
There are some differences between modulo operation and remainder operation,% means remainder in C/C++ and Java,but modulo in Python.
++/– can be on the left side or the right side of the number,but the output is different
Ex.1
1 | //Left side |
Output
1 | a:6 |
Ex.2
1 | //Right side |
Output
1 | a:6 |
Both increment operator and decrement operator can only use to operate variables,you can’t directly on values or constants.
Ex
1 | 5++;//Wrong |
Assignment operators
Assignment operators is a kind of notation that use to assign value for variables and constants
Common assignment operators in Java:
| Operator | Definition | Example |
|---|---|---|
| = | Assignment | c = a + b,assign the sum of a & b to c |
| += | Addition | a += b,assign the sum of a & b to a |
| -= | Subtraction | a -= b,assign the margin of a - b to a |
| *= | Multiplication | a *= b,assign the product of a & b to a |
| /= | Division | a /= b,assign the quotient of a & b to a |
| %= | Modulus | a %= b,assign the remainder of a divide by b to a |
Comparison operators
Comparsion operators is use to compare two values,and output a boolean value(true or false)
Common comparsion operators in Java:
| Operator | Definition | Example | Output |
|---|---|---|---|
| > | Greater than | a = 5;b = 2;a > b | true |
| < | Less than | a = 5;b = 2;a < b | false |
| >= | Greater or equal | a = 5;a >= 3 | true |
| <= | Less or equal | a = 5;b = 2;b <= a | true |
| == | Equal to | a = 5;b = 2;a == b | false |
| != | Not equal to | b = 2;b != 2 | true |
Notice:
, < , >= , <= can only support numeric type on both sides of the operators
== , != can support numeric type and reference type on both sides
Logical operators
It’s mainly use for logical operations.
Common logical operators in Java:
| Logical operator | Definition | Example | Output |
|---|---|---|---|
| && | Logical and | a && b | if a and b both are true,then return true |
| || | Logical or | a || b | if one of a and b is true,then return true |
| ! | Logical not | !a | if a is false,then return true |
| ^ | Exclusive or | a ^ b | if a and b only have one of them is true,then return true |
Conditional operator
Conditional operator is also called Ternary Operator
- syntax: Boolean expression ? expression 1 :expression 2
If the value of boolean expression is true,then return the value of expression 1;If it’s false,then return the value of expression 2.
Ex1
2
3
4int result = (8 > 5) ? 1 : 0;
System.out.println(result);
//output
//1
The priority level of operators
| Priority | Operators |
|---|---|
| 1 | () |
| 2 | var++ , var– |
| 3 | +(positive) , -(negative) , ++var , –var |
| 4 | (type)(type convert) |
| 5 | !(not) |
| 6 | * , / , % |
| 7 | +(plus) , -(minus) |
| 8 | < , <= , > , >= |
| 9 | == , != |
| 10 | ^ |
| 11 | && |
| 12 | || |
| 13 | ?: |
| 14 | = , += , -= , *= , /= , %= |
Level 1 has the highest priority and level 11 has the lowest.
In real life,we usually use brackets to assist priority management.