In this tutorial post, we will study and understand the concept of Operators in Java & Operator Precedence. We will see a few examples to understand these concepts.
Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc. There are many types of operators in java which are given below:
- Unary Operator
- Arithmetic Operator
- shift Operator
- Relational Operator
- Bitwise Operator
- Logical Operator
- Ternary Operator
- Assignment Operator
Java Unary Operator
The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:
- incrementing/decrementing a value by one
- negating an expression
- inverting the value of a boolean
class OperatorExample{ public static void main(String args[]){ int x=10; System.out.println(x++);//10 (11) System.out.println(++x);//12 System.out.println(x--);//12 (11) System.out.println(--x);//10 } }
12
12
10
class OperatorExample{ public static void main(String args[]){ int a=10; int b=-10; boolean c=true; boolean d=false; System.out.println(~a);//-11 (minus of total positive value which starts from 0) System.out.println(~b);//9 (positive of total minus, positive starts from 0) System.out.println(!c);//false (opposite of boolean value) System.out.println(!d);//true } }
9
false
true
Java Arithmetic Operators
Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.
class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; System.out.println(a+b);//15 System.out.println(a-b);//5 System.out.println(a*b);//50 System.out.println(a/b);//2 System.out.println(a%b);//0 } }
5
50
2
0
Java AND Operator Example: Logical && and Bitwise &
The logical && operator doesn’t check second condition if first condition is false. It checks second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a<b&&a<c);//false && true = false System.out.println(a<b&a<c);//false & true = false } }
false
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn’t check second condition if first condition is true. It checks second condition only if first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a>b||a<c);//true || true = true System.out.println(a>b|a<c);//true | true = true //|| vs | System.out.println(a>b||a++<c);//true || true = true System.out.println(a);//10 because second condition is not checked System.out.println(a>b|a++<c);//true | true = true System.out.println(a);//11 because second condition is checked } }
true
true
10
true
11
Java Ternary Operator
Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot in java programming. it is the only conditional operator which takes three operands.
class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a<b)?a:b; System.out.println(min); } }
Java Assignment Operator
Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.
class OperatorExample{ public static void main(String args[]){ int a=10; int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(a); System.out.println(b); } }
16
Java Operator Precedence
Operator precedence is the order in which operator operate on variables and expression. For e.g. if there are multiple operators in a single expression, which operator operates first matters as the final output value depends in such scenario. This is similar to the BOARD MASS rule that we apply in mathematics. Lets take an example –
class Precedence { public static void main(String[] args) { int a = 10, b = 5, c = 1, result; result = a-++c-++b; System.out.println(result); } }
The operator precedence of prefix ++ is higher than that of – subtraction operator. Hence,
result = a-++c-++b;
is equivalent to
result = a-(++c)-(++b);
Following is the Operator Precedence table. The operators are sorted in their precedence order i.e. Unary operator has the highest precedence and assignment has the lowest.
Operator Type | Category | Precedence |
Unary | postfix | expr++ expr– |
prefix | ++expr –expr +expr -expr ~ ! | |
Arithmetic | multiplicative | * / % |
additive | + – | |
Shift | shift | << >> >>> |
Relational | comparison | < > <= >= instanceof |
equality | == != | |
Bitwise | bitwise AND | & |
bitwise exclusive OR | ^ | |
bitwise inclusive OR | | | |
Logical | logical AND | && |
logical OR | || | |
Ternary | ternary | ? : |
Assignment | assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |