Operators in Java & Operator Precedence with Program Example

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

Run Online

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  
}
}
Output
10
12
12
10

Run Online

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  
}
}
Output
-11
9
false
true
Java Arithmetic Operators

Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.

Run Online

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  
}
}
Output
15
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.

Run Online

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  
}
}
Output
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.

Run Online

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  
}
}
Output
true
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.

Run Online

class OperatorExample{  
public static void main(String args[]){  
int a=2;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);  
}
}
Output
2
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.

Run Online

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);  
}
}
Output
14
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 –

Run Online

class Precedence {
    public static void main(String[] args) {
    	
    	int a = 10, b = 5, c = 1, result;
    	result = a-++c-++b;
    	
    	System.out.println(result);
    }
}
Output
2

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 TypeCategoryPrecedence
Unarypostfixexpr++ expr–
prefix++expr –expr +expr -expr ~ !
Arithmeticmultiplicative* / %
additive+ –
Shiftshift<< >> >>>
Relationalcomparison< > <= >= instanceof
equality== !=
Bitwisebitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
Logicallogical AND&&
logical OR||
Ternaryternary? :
Assignmentassignment= += -= *= /= %= &= ^= |= <<= >>= >>>=
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *