Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

More on operators and expressions

Fundamentals of programming I | Student is obliged to write down and run this program by himself | Data in a program (variables and literals) | Operations on data (operators and expressions) | Fundamentals of programming II | Write down and run this program on your computer | Write down and run this program on your computer | Introduction to objects | The first program | Types. Primitive types. |


Читайте также:
  1. Absence of the articles in set expressions
  2. Additional Words and Expressions
  3. And some useful expressions
  4. Anonymous function expressions
  5. Anonymous object creation expressions
  6. Array creation expressions
  7. Assignment 5. Fill in the gaps with the words and expressions from the text.

In complex expressions (made up of a number of operators) evaluation order of operators depends on operator precedence and how they group.

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 arguments, 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:

Moreover:


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:


 


Дата добавления: 2015-11-16; просмотров: 60 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Types of variables. Declarations.| Numeric promotions

mybiblioteka.su - 2015-2024 год. (0.006 сек.)