Transcript Operators

Lecture 3
Operators
MIT AITI - 2003
What are Operators?
• Operators are the most basic units of a program.
They perform operations on primitive data types.
• Operators are special symbols used for
mathematical functions, assignment statements,
and logical comparisons
• As you know, expressions are statements that
produce a value. Some of the most common
expressions are mathematical and make use of
operators
• Today we will go over 5 different groups of
operators:
– Arithmetic operators
– Assignment operator
– Relational operators
– Logical operators
– Increment/Decrement operators
As you learn more about Java, you
will learn additional operators
Arithmetic Operators
• Java has 4 basic arithmetic operators
+,-,*, and /, which have the usual
meanings – add, subtract, multiply, and
divide.
• The order of operations or precedence
that applies when an expression using
these operators is evaluated is the same
as you learned at school.
•
For example:
1. int number = 20 – 3*3 – 9/3 ;
will produce the value 8
2. int number = (20 – 3)*(3 – 9)/3;
is equivalent to 17*(-6)/3 will produce the
value -34
Assignment Operator
•
•
Assignment operators include = as well as the
arithmetic operators (+,-,/,*, and %) used in
conjunction with it.
For example:
1.
2.
count += 5;
is equivalent to
count = count + 5;
result /= a % b/(a+b);
is equivalent to
result = result / (a % b/(a+b));
Relational Operators
Operator
Description
>
Produces the value true if the left operand is greater
than the right operand, and false otherwise
>=
Produces the value true if the left operand is greater
than or equal to the right operand, and false otherwise
==
Produces the value true if the left operand is equal to
the right operand, and false otherwise
!=
Produces the value true if the left operand is not equal
to the right operand, and false otherwise
<=
Produces the value true if the left operand is less than
or equal to the right operand, and false otherwise
<
Produces the value true if the left operand is less than
the right operand, and false otherwise
•
For example:
int x = 3;
int y = 5;
boolean state;
1.
state = (x > y);
now state is assigned the value false because x is not greater than
y
3.
state = (15 == x*y);
now state is assigned the value true because the product of x and y
equals 15
4.
state = (x != x*y);
now state is assigned the value true because the product of x and y
is not equal to x
Logical Operators
Symbol
Long Name
&
Logical AND
&&
Conditional AND
|
Logical OR
||
Conditional OR
!
Logical negation (NOT)
Logical operators are only used to combine expressions that have a
value of true or false. Because they operate on boolean values
they are also referred to as boolean operators.
Truth Table for Logical Operators
x
y
x|y
x || y
True
!x
True
x&y
x && y
True
True
True
False
False
True
False
False
True
False
True
True
False
False
False
False
True
False
•
For example:
boolean x = true;
boolean y = false;
boolean state;
1. Let state = (x & y);
now state is assigned the value false (see truth
table!)
2. Let state = ((x | y) & x);
now state is assigned the value true
(x | y) gives true (see truth table!)
and then (true & x) gives true
•
boolean x = true;
boolean y = false;
boolean state;
1. Let state = (((x || y) | (!x)) & ((!
y) & x))
now state is assigned the value true
(x II y) gives true; (! x) gives false;
so ((x || y) | (! x)) is equivalent to (true | false) which
gives true.
(! y) gives true;
so ((! y) & x) is equivalent to (true & x) which gives
true.
So the whole expression for state is equivalent to
(true & true), that state is true.
&& versus &
• The difference between && and & is that
the conditional && will not bother to
evaluate the right-hand operator if the lefthand operator is false, since the result is
already determined in this case to be
false. The logical & operator will evaluate
both terms of the expression.
|| versus |
• The difference between || and | is that the
conditional || will not bother to evaluate the
right-hand operator if the left-hand
operator is true, since the result is
already determined in this case to be true.
The logical | operator will evaluate both
terms of the expression.
We will come back to this example after we learn about control
structures.
• As we saw in the previous examples in most
cases the two operators are interchangeable.
Here are examples of where it matters which
operator we use.
• if (++value%2 == 0 & ++ count < limit) {
// Do something
}
Here the variable count will be incremented in
any event. If you use && instead of &, count
will only be incremented if the left operand of
the AND operator is true. You get a different
result depending on which operator is used.
• if (count > 0 && total/ count > 5) {
// Do something
}
In this case the right operand for the &&
operation will only be executed if the left
operand is True – that is, when count is
positive. Clearly, if we were to use & here,
and count happened to be zero, we will
be attempting to divide the value of total
by 0, which will terminate the program.
Increment/Decrement Operators
• Let int count;
count = count + 1;
The statement above can be written as
++count; //This operator is called the increment
operator.
• The decrement operator has the same
effect on count for subtracting 1.
count = count - 1;
--count;
• The increment/decrement operator has two forms:
– The prefix form ++count, --count
first adds 1 to the variable and then continues to any other
operator in the expression
int numOrange = 5;
int numApple = 10;
int numFruit;
numFruit = ++numOrange + numApple;
numFruit has value 16
numOrange has value 6
– The postfix form count++, count-first evaluates the expression and then adds 1 to the variable
int numOrange = 5;
int numApple = 10;
int numFruit;
numFruit = numOrange++ + numApple;
numFruit has value 15
numOrange has value 6
Conditional operator
• The conditional operator is:
? :
You will learn about the uses of the
conditional operator in following lectures
Precedence of Operators
• Precedence specifies the order in which an
operation is performed
• Consider the expression: a + b * c
The multiplication is carried out first, then the
value of b * c is added to a
• Here is a table which outlines the precedence of
operators in Java and their associativity (left to
right or right to left)
Note: Some of these operators will not be
familiar to you, you will learn more about them in
following lectures
P = Precedence A = Associativity
P
A
Operator
P
A
Operator
15
L
. , [ ] , (args) , ++, --
7
L
&
14
R
++ , -- , + , - , ~ , !
6
L
^
13
R
new , (type)
5
L
|
12
L
*,/,%
4
L
&&
11
L
+,-
3
L
||
10
L
<< , >> , >>>
2
R
?:
9
L
< , <= , > , >=
1
R
8
L
== , !=
= , *= , /= , %= , += , -=
<<= , >>= , >>>= , &=
^= , |=
POP QUIZ
•
1)What is the value of number?
int number = 5*3–3/6–9*3;
2) What is the value of state?
int x = 8;
int y = 2;
boolean state;
state = (15 == x*y);
3) What is the value of state?
boolean x = false;
boolean y = true;
boolean state;
state = (((x && y) & (!x)) || ((! y) | x))
4) What is the value of numCar?
What is the value of numGreeenCars?
int numBlueCars = 5;
int numGreenCars = 10;
int numCar;
numCar = numGreenCars++ + numBlueCars + ++numGreeenCars;