Java Operators
An operator performs an operation on a given variable or literal. The target processed by an operator is called an operand.
Operators can be classified as assignment operators, arithmetic operators, bit operators, comparison or relational operators, logical operators, and so on.
Assignment Operator
The assignment operator can be a little confusing for people new to programming. In general mathematics, the single equals sign = is used only to express equality, but in programming it means assigning a value to a variable.
The examples below assign a value or expression to a variable.
- variable = value
int a = 1; // 1 is assigned to variable a.
- variable = expression
int a = 1 + 2; // 1+2 is calculated, and 3 is assigned to variable a.
- variable = variable = variable… = value(or expression)
int a, b, c;
a = b = c = 100 // 100 is assigned equally to variables a, b, and c.
The data type of the variable must be the same as, or compatible with, the expression.
int a = 1;
boolean b = true;
int c = a + b; // compile error
Arithmetic Operators
Arithmetic operators are used in arithmetic expressions in the same way as in ordinary mathematics.
Basic Arithmetic Operators
Arithmetic operations can be performed on all numeric types, such as addition, subtraction, multiplication, and division.
| Operator | Feature | Usage | Description |
|---|---|---|---|
| + | Addition | op1 + op2 | Adds op1 and op2. |
| - | Subtraction(or unary minus) | op1 - op2 | Subtracts op2 from op1. |
| * | Multiplication | op1 * op2 | Multiplies op1 and op2. |
| / | Division | op1 / op2 | Divides op1 by op2. |
| % | Remainder | op1 % op2 | Gets the remainder after dividing op1 by op2. |
Example 1
package com.devkuma.tutorial.operators;
public class Arithmetic {
public static void main(String[] args) {
int a = 1 + 3;
int b = a - 2;
int c = b * 5;
int d = c / 2;
int e = d % 3;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("e=" + e);
}
}
Execution result:
a=4
b=2
c=10
d=5
e=2
Increment and Decrement Operators
The increment operator increases by one, and the decrement operator decreases by one.
The result differs depending on where the operator is placed. For example, assuming the initial value of op is 2, if the operator comes before it as ++op, it increments first and returns 3. If the operator comes after it as op++, it returns the value before incrementing, 2, and then op becomes 3.
| Operator | Feature | Usage | Description |
|---|---|---|---|
| ++ | Prefix increment | ++op | Increases op by 1 and returns the value after incrementing. |
| Postfix increment | op++ | Increases op by 1 and returns the value before incrementing. | |
| – | Prefix decrement | –op | Decreases op by 1 and returns the value after decrementing. |
| Postfix decrement | op– | Decreases op by 1 and returns the value before decrementing. |
Example 2
package com.devkuma.tutorial.operators;
public class Increase {
public static void main(String[] args) {
int a = 10;
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
}
}
Execution result:
10
12
12
10
Arithmetic Assignment Operators
These operators combine the assignment operator with other arithmetic operators.
For example, a += 2 adds 2 to a and assigns the result back to a.
| Operator | Feature | Usage | Meaning |
|---|---|---|---|
| += | Addition assignment | op1 += op2 | op1 = op1 + op2 |
| -= | Subtraction assignment | op1 -= op2 | op1 = op1 - op2 |
| *= | Multiplication assignment | op1 *= op2 | op1 = op1 * op2 |
| /= | Division assignment | op1 /= op2 | op1 = op1 / op2 |
| %= | Remainder assignment | op1 %= op2 | op1 = op1 % op2 |
Example 3
package com.devkuma.tutorial.operators;
public class AssignmentOperator {
public static void main(String[] args) {
int a = 4;
int b = 5;
int c = 6;
a += 4;
b -= 2;
c += a * b;
c %= 7;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
}
}
The execution result is as follows.
a=8
b=3
c=2
Bit Operators
Basic Bit Operators
Bit operators perform binary bit operations.
When performing an &(AND) operation on 3 and 6, each bit of 0011(3) and 0110(6) is ANDed, resulting in 0010, which is 2.
- 0011(3) & 0110(6) = 0010(2)
| Operator | Feature | Usage | Description |
|---|---|---|---|
& |
AND | op1 & op2 | Bitwise logical product |
| ` | ` | OR | op1 | op2 |
^ |
XOR | op1 ^ op2 | Bitwise exclusive OR |
~ |
NOT | ~op1 | Bitwise negation |
Example 4
package com.devkuma.tutorial.operators;
public class Bit {
public static void main(String[] args) {
int a = 3;
int b = 6;
int c = a & b;
int d = a | b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("e=" + e);
System.out.println("f=" + f);
}
}
Execution result:
c=2
d=7
e=5
f=5
Shift Operators
- Shift operators include left shift, right shift, and unsigned right shift.
- The unsigned right shift operator is used only on 32-bit and 64-bit values.
| Operator | Feature | Usage | Description |
|---|---|---|---|
| » | Right shift | op1 » op2 | Shifts op1 to the right by op2. |
| « | Left shift | op1 « op2 | Shifts op1 to the left by op2. |
| »> | Unsigned right shift | op1 »> op2 | Shifts op1 to the right by op2 and always fills the left side with 0 regardless of sign. |
Example 5
package com.devkuma.tutorial.operators;
public class Shift {
public static void main(String[] args) {
// Moving 1000(8) two bits to the right becomes 100000(32).
byte a = 8 << 2;
// Moving 1000(8) two bits to the left becomes 10(2).
byte b = 8 >> 2;
// -1 as 32 bits is 11111111111111111111111111111111,
// and shifting it right by 2 always fills the left side with 0 regardless of sign.
// Therefore, the value becomes 01111111111111111111111111111111(2147483647).
int c = -1 >>> 1;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
}
}
Execution result:
a=32
b=2
c=2147483647
Bit Assignment Operators
These operators combine the assignment operator with other bit operators.
| Operator | Feature | Usage | Meaning |
|---|---|---|---|
&= |
AND assignment | op1 &= op2 | op1 = op1 & op2 |
| ` | =` | OR assignment | op1 |= op2 |
^= |
XOR assignment | op1 ^= op2 | op1 = op1 ^ op2 |
>>= |
» assignment | op1 »= op2 | op1 = op1 « op2 |
<<= |
« assignment | op1 «= op2 | op1 = op1 « op2 |
>>>= |
»> assignment | op1 »>= op2 | op1 = op1 »> op2 |
Logical Result Table for Bit Operators
| A | B | A&B | A|B | A^B | ~A |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 | 0 |
Comparison(Relational) Operators
Comparison, or relational, operators are binary operators that determine the relationship one operand has with another operand.
Because the result of these operations is always a boolean value(true or false), they are often used in control statements such as if statements and loop statements such as for statements.
| Operator | Feature | Usage | Description |
|---|---|---|---|
> |
Greater than | op1 > op2 |
true if op1 is greater than op2; otherwise false |
>= |
Greater than or equal to | op1 >= op2 |
true if op1 is greater than or equal to op2; otherwise false |
< |
Less than | op1 < op2 |
true if op1 is less than op2; otherwise false |
<= |
Less than or equal to | op1 <= op2 |
true if op1 is less than or equal to op2; otherwise false |
== |
Equal to | op1 == op2 |
true if op1 and op2 are equal; otherwise false |
!= |
Not equal to | op1 != op2 |
true if op1 and op2 are not equal; otherwise false |
instanceof |
Same instance | op1 instanceof op2 |
true if op1 is an instance(object) of op2; otherwise false |
Example 6
package com.devkuma.tutorial.operators;
public class Relation {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
boolean d = a > b;
boolean e = a >= b;
boolean f = a < b;
boolean g = a <= c;
boolean h = a == c;
boolean i = a != b;
System.out.println("d=" + d);
System.out.println("e=" + e);
System.out.println("f=" + f);
System.out.println("g=" + g);
System.out.println("h=" + h);
System.out.println("i=" + i);
}
}
Execution result:
d=false
e=false
f=true
g=true
h=true
i=true
Logical Operators
Logical operators evaluate operand values and return true or false as the result.
A notable point is the short-circuit logical operators &&(logical AND) and ||(logical OR). Unlike the single-symbol & and | operators, if the result of evaluating the first operand means the second operand does not need to be evaluated, the result is returned immediately. Therefore, if some processing exists in the second operand and it is not evaluated, that processing will not run.
For example, in the expression below, if flag is true, the expression count > index++ is not processed, so index does not increase. Conversely, if flag is false, index increases by 1.
(flag == true) || (count > index++)
| Operator | Feature | Usage | Description |
|---|---|---|---|
& |
Logical AND | op1 & op2 |
true if both op1 and op2 are true; evaluates both op1 and op2. |
| ` | ` | Logical OR | op1 | op2 |
^ |
Logical XOR(exclusive OR) | op1 ^ op2 |
false if either op1 or op2 is true; evaluates both op1 and op2. |
&& |
Short-circuit AND | op1 && op2 |
true if both op1 and op2 are true; if op1 is false, op2 is not evaluated. |
| ` | ` | Short-circuit OR | |
! |
Logical unary NOT | !op |
false if op is true, true if op is false |
Example 7
package com.devkuma.tutorial.operators;
public class Logic {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
boolean c = true;
boolean d = a & b;
boolean e = a | b;
boolean f = a ^ b;
boolean g = a && b;
boolean h = a && c;
boolean i = a || b;
boolean j = a || c;
boolean k = !a;
System.out.println("d=" + d);
System.out.println("e=" + e);
System.out.println("f=" + f);
System.out.println("g=" + g);
System.out.println("h=" + h);
System.out.println("i=" + i);
System.out.println("j=" + j);
System.out.println("k=" + k);
}
}
Execution result:
d=false
e=true
f=true
g=false
h=true
i=true
j=true
k=false
Logical Result Table for Logical Operators
A |
B |
A&B |
A|B |
A^B |
~A |
|---|---|---|---|---|---|
| false | false | false | false | false | true |
| true | false | false | true | true | false |
| false | true | false | true | true | true |
| true | true | true | true | false | false |
Logical Assignment Operators
These operators combine the assignment operator with other logical operators.
| Operator | Feature | Usage | Meaning |
|---|---|---|---|
&= |
Logical AND assignment | op1 &= op2 |
op1 = op1 & op2 |
| ` | =` | Logical OR assignment | op1 |= op2 |
^= |
Logical XOR assignment | op1 ^= op2 |
op1 = op1 ^ op2 |
Ternary Operator
The ternary operator ?:, which has three operands, can be used as a shortened form of an if-then-else selection statement.
- condition ? expression1(if the condition is true) : expression2(if the condition is false)
It evaluates the condition and determines true or false. If true, it executes expression1; if false, it executes expression2 and returns the result. It is useful with assignment operators instead of conditional statements(if).
Example 3
public class Three {
public static void main(String[] args) {
int a = 2;
int b = 3;
int max = (a > b) ? a : b;
System.out.println("max=" + max);
}
}
Execution result:
max=3
Operator Precedence
Regardless of precedence, you can specify priority by enclosing part of an expression in parentheses ().
| Precedence | Operator |
|---|---|
| 1 | (), [], . |
| 2 | ++, --, ~, ! |
| 3 | *, /, % |
| 4 | +, - |
| 5 | >>, >>>, << |
| 6 | >, >= , <, <= |
| 7 | ==, != |
| 8 | & |
| 9 | ^ |
| 10 | ` |
| 11 | && |
| 12 | ` |
| 13 | ?: |
| 14 | =, op= |