0% found this document useful (0 votes)
25 views

Java Operator Precedence - Javatpoint

The document discusses Java operator precedence by explaining what operator precedence is, the precedence and associativity of different Java operators, and providing examples. Operator precedence determines the grouping of operators and operands in an expression and how it will evaluate. Higher precedence operators are evaluated first unless parentheses are used.

Uploaded by

ishwar khalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Operator Precedence - Javatpoint

The document discusses Java operator precedence by explaining what operator precedence is, the precedence and associativity of different Java operators, and providing examples. Operator precedence determines the grouping of operators and operands in an expression and how it will evaluate. Higher precedence operators are evaluated first unless parentheses are used.

Uploaded by

ishwar khalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/22/24, 8:42 PM Java Operator Precedence - Javatpoint

ADVERTISEMENT

Java Operator Precedence


In this section, we will learn the operator precedence in Java along with examples.

What is operator precedence?


The operator precedence represents how two expressions are bind together. In an expression,
it determines the grouping of operators with operands and decides how an expression will
evaluate.

While solving an expression two things must be kept in mind the first is a precedence and the
second is associativity.

Precedence

Precedence is the priority for grouping different types of operators with their operands. It is
meaningful only if an expression has more than one operator with higher or lower precedence.
The operators having higher precedence are evaluated first. If we want to evaluate lower
precedence operators first, we must group operands by using parentheses and then evaluate.

https://www.javatpoint.com/java-operator-precedence 2/9
4/22/24, 8:42 PM Java Operator Precedence - Javatpoint

ADVERTISEMENT

Associativity

We must follow associativity if an expression has more than two operators of the same
precedence. In such a case, an expression can be solved either left-to-right or right-to-left,
accordingly.

Java Operator Precedence Table


The following table describes the precedence and associativity of operators used in Java.

Precedence Operator Type Associativity

15 () Parentheses Left to Right


[] Array subscript
· Member selection

14 ++ Unary post-increment Right to left


-- Unary post-decrement

13 ++ Unary pre-increment Right to left


-- Unary pre-decrement
+ Unary plus
- Unary minus
! Unary logical negation
~ Unary bitwise complement
(type) Unary type cast

https://www.javatpoint.com/java-operator-precedence 3/9
4/22/24, 8:42 PM Java Operator Precedence - Javatpoint

12 * Multiplication Left to right


ADVERTISEMENT
/ Division
% Modulus

11 + Addition Left to right


- Subtraction

10 << Bitwise left shift Left to right


>> Bitwise right shift with sign extension
>>> Bitwise right shift with zero extension

9 < Relational less than Left to right


<= Relational less than or equal
> Relational greater than
>= Relational greater than or equal
instanceof Type comparison (objects only)

8 == Relational is equal to Left to right


!= Relational is not equal to

7 & Bitwise AND Left to right

6 ^ Bitwise exclusive OR Left to right

5 | Bitwise inclusive OR Left to right

4 && Logical AND Left to right

3 || Logical OR Left to right

2 ?: Ternary conditional Right to left

1 = Assignment Right to left


+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment

Note: Larger the number higher the precedence.

https://www.javatpoint.com/java-operator-precedence 4/9
4/22/24, 8:42 PM Java Operator Precedence - Javatpoint

Java Operator Precedence Example


ADVERTISEMENT

Let's understand the operator precedence through an example. Consider the following
expression and guess the answer.

1+5*3

You might be thinking that the answer would be 18 but not so. Because the multiplication (*)
operator has higher precedence than the addition (+) operator. Hence, the expression first
evaluates 5*3 and then evaluates the remaining expression i.e. 1+15. Therefore, the answer will
be 16.

Let's see another example. Consider the following expression.

x+y*z/k

In the above expression, * and / operations are performed before + because of precedence. y is
multiplied by z before it is divided by k because of associativity.
ADVERTISEMENT

← Prev Next →

ADVERTISEMENT

https://www.javatpoint.com/java-operator-precedence 5/9

You might also like