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

Operator Precedence (JAVA)

Operator precedence determines the order that operators are evaluated in an expression. For example, in the expression 3 * 4 - 1, the multiplication operator * has higher precedence than the subtraction operator -, so it is evaluated as (3 * 4) - 1 rather than 3 * (4 - 1). Operator precedence is defined in a table that lists operators from highest to lowest precedence. If two operators have equal precedence, the expression is evaluated based on the operator's associativity, either from left to right or right to left. These precedence rules ensure expressions are evaluated consistently and predictably.

Uploaded by

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

Operator Precedence (JAVA)

Operator precedence determines the order that operators are evaluated in an expression. For example, in the expression 3 * 4 - 1, the multiplication operator * has higher precedence than the subtraction operator -, so it is evaluated as (3 * 4) - 1 rather than 3 * (4 - 1). Operator precedence is defined in a table that lists operators from highest to lowest precedence. If two operators have equal precedence, the expression is evaluated based on the operator's associativity, either from left to right or right to left. These precedence rules ensure expressions are evaluated consistently and predictably.

Uploaded by

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

Operator Precedence

[email protected]
Operator precedence determines the order in which the operators in an expression are
evaluated.

For eg –
int x = 3 * 4 - 1;
In the above example, the value of x will be 11, not 9. This happens because the
precedence of * operator is higher than - operator. That is why the expression is
evaluated as (3 * 4) - 1 and not 3 * (4 - 1).

Operator Precedence Table

Operators Precedence
postfix increment and decrement
prefix increment and decrement, and
unary
multiplicative

additive

shift

relational

equality

bitwise AND

bitwise exclusive OR

bitwise inclusive OR

logical AND

logical OR

ternary
assignment

Associativity of Operators
If an expression has two operators with similar precedence, the expression is
evaluated according to its associativity (either left to right, or right to left).

[email protected]
Operators Precedence Associativity
postfix increment and
left to right
decrement
prefix increment and
right to left
decrement, and unary
multiplicative left to right

additive left to right

shift left to right

relational left to right

equality left to right

bitwise AND left to right

bitwise exclusive OR left to right

bitwise inclusive OR left to right

logical AND left to right

logical OR left to right

ternary right to left

assignment right to left

Note - These notes are just for a quick glance. We don’t have to memorize them all at
once. Most of these rules are very logical and we have been following them in a lot of
instances already.

You might also like