﻿﻿ More on operators and expressions

# More on operators and expressions

## Operator precedence

determines the order of evaluation of different operations contained in an expression (i.e. in the expression a + b * c, is addition or multiplication to be evaluated first?).

Operators with the same precedence may be evaluated either left-to-right or right-to-left. The ones that group left-to-right are called

## left-associative

, those that group right-to-left are called

## right-associative

. For example, the expression a + b - c may be evaluated as (a+b)-c or a+(b-c). Although in this case grouping does not matter, there are situations when it is important.

Operators have

, also called

## operands

.
In Java there are many unary and binary operators and one operator which takes three arguments.
Arguments for operators are always expressions. In the expression
a + (b - 1)
arguments of the binary + are the expressions a and (b-1),
in the expression b-1, arguments to binary - are the expressions b and 1.

The Java language defines the following operators:

Precedence, grouping Operator Name 1, right-associative ! Logical Complement ~ Bitwise Complement + Unary Plus - Unary Minus ++ Increment -- Decrement (type) Cast Operator 2, left-associative * Multiplication / Division % Remainder 3, left-associative + Addition - Subtraction 4. left-associative << Left Shift >> Right Shift >>> Unsigned Right Shift 5, left-associative < Relational Operators <= >= > instanceof Type Comparison Operator 6, left-associative == Equality Operators != 7. & Bitwise AND 8. ^ Bitwise XOR (exclusive OR) 9. | Bitwise OR 10. && Conditional AND 11. || Conditional OR 12, right-associative ?: Conditional Operator 13. right-associative = =* =/ =% =+ =- =& =^ =| =<> Assignment Operators

Note:
lower numbers in precedence column specify higher precedence
unary operators have blue background, binary operators - white background

## Assignment operators

are right-associative. It means that the expression:

x = a + b

is evaluated from right to left. It does not make any difference here, but consider an expression which itself is an assignment (the value of assignment expression is the value of its left-hand side variable) and its value is assigned to some variable:

int x, a = 3, b = 4;
int c = 1;
x = a = b = c + 7

Here, the precedence of binary + is higher than that of assignment operator =. Thus the expression c + 7 is evaluated first, and its value is assigned to the variable b. The value of the expression b = c + 7 is now 8 and it is assigned to the variables on the left-hand side: a and then x. Consequently, the variables x, a, and b store the same value: 8.

In the above table there are a number of the so called Compound Assignment Operators of the form:

## op=

where op is one of the operators:

## * / % + - <> >>> & ^ |

These operators are applied the following way:

## x op= expression

is the abbreviated form of:

## x = x op ( expression )

where:
x - a variable
expression - an arbitrary expression
op - operator

Thus, instead of:

numOfChildren = numOfChildren + 1

we can write:

numOfChildren += 1

There are two kinds of unary increment (++) and decrement (--) operators:

• postfix (the operator is placed after its argument)
• prefix (the operator is placed before its argument)

Moreover:

• ++ increases and -- decreases the value of operand

## by 1

.
• the value of the expression with the

## prefix

operator is the value of its argument

## after

application of the operator (increment or decrement)
• the value of the expression with the

## postfix

operator is the value of its argument

## before

application of the operator - it equals the original value of the argument.

Thus, after the evaluation of the following:

int n, i = 1;
n = i++; // postfix ++

the value of n is 1, but the value of a is 2.

However, after
int n, i = 1;
n = ++i; // prefix ++

the value of both n and i is 2.

The following program explains the above remarks:

public class Express1 { public static void main(String[] args) { int a = 1, b = 2, c = 3; a = b = c * 1 + 2; System.out.println("a=" + a + " b=" + b + " c=" + c); a = b = c * (1 + 2); System.out.println("a=" + a + " b=" + b + " c=" + c); a = b++; System.out.println("a=" + a + " b=" + b + " c=" + c); c = --b; System.out.println("a=" + a + " b=" + b + " c=" + c); a++; b++; c++; System.out.println("a=" + a + " b=" + b + " c=" + c); a = b++*++c; System.out.println("a=" + a + " b=" + b + " c=" + c); int longVariableName = 20; longVariableName = longVariableName * 10; longVariableName *= 10; System.out.println( longVariableName ); }}

a=5 b=5 c=3
a=9 b=9 c=3
a=9 b=10 c=3
a=9 b=9 c=9
a=10 b=10 c=10
a=110 b=11 c=11
2000

The binary + operator is used here not only to add numbers, but also to concatenate strings. Further discussion of this feature will be presented in the next lecture.

Here is the output of the program:

Последнее изменение этой страницы: 2018-09-12;

weddingpedia.ru 2018 год. Все права принадлежат их авторам! Главная