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

Output
10
12
12
10

Run Online

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

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

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

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

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

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

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 *