P. Raveena Lakshmi, M.Sc., M.Phil.,
Assistant Professor
Operators
Types of Operators
 Java provides a rich set of operators to manipulate
variables. We can divide all the Java operators into the
following groups:
 Arithmetic Operators
 Relational Operators
 Increment & Decrement Operators
 Bitwise Operators
 Logical Operators
 Assignment Operators
 Conditional Opeartors
2-3/30
Arithmetic Operators
 Arithmetic operators are used in mathematical expressions
in the same way that they are used in algebra. Assume
integer variable A holds 10 and variable B holds 20, then:
Operator Description Example
+ Addition - Adds values on either side of
the operator
A + B will give 30
- Subt raction - Subt racts right hand
operand from left hand operand
A - B will give -10
* Multiplication - Multiplies values on
either side of the operator
A * B will give 200
/ Division - Divides left hand operand by
right hand operand
B / A will give 2
% Modulus - Divides left hand operand by
right hand operand and returns
remainder
B % A will give 0
Relational Operators
 There are following relational operators supported by Java
language Assume variable A holds 10 and variable B holds
20, then:
Operator Description Example
== Checks if the values of two operands are
equal or not, if yes then condition
becomes t rue.
(A == B) is not t rue.
!= Checks if the values of two operands are
equal or not, if values are not equal
then condition becomes t rue.
(A != B) is t rue.
> Checks if the value of left operand is
greater than the value of right operand,
if yes then condition becomes t rue.
(A > B) is not t rue.
Operator Description Example
< Checks if the value of left operand is less
than the value of right operand, if yes
then condition becomes t rue.
(A < B) is t rue.
>= Checks if the value of left operand is
greater than or equal to the value of
right operand, if yes then condition
becomes t rue.
(A >= B) is not true.
<= Checks if the value of left operand is less
than or equal to the value of right
operand, if yes then condition becomes t
rue.
(A <= B) is t rue.
2-6/30
Increment and Decrement
 The increment and decrement operators use only one
operand
 The increment operator (++) adds one to its operand
 The decrement operator (--) subtracts one from its
operand
 The statement
count++;
is functionally equivalent to
count = count + 1;
Increment and Decrement
 The increment and decrement operators can be
applied in postfix form:
count++
 or prefix form:
++count
Bitwise Operators
 Java defines several bitwise operators, which can be applied
to the integer types, long, int, short, char, and byte.
 Bitwise operator works on bits and performs bit-by-bit
operation. Assume if a = 60; and b = 13; now in binary
format they will be as follows:
a = 0011 1100
b = 0000 1101
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
2-9/30
Logical Operators
 The following table lists the logical operators: Assume
Boolean variables A holds t rue and variable B holds
false, then:
Operator Description Example
&& Called Logical AND operator. If both the
operands are non-zero, then the condition
becomes t rue.
(A && B) is false.
|| Called Logical OR Operator. If any of the
two operands are nonzero, then the
condition becomes t rue.
(A || B) is t rue.
! Called Logical NOT Operator. Use to
reverses the logical state of its operand. If
a condition is t rue then Logical NOT
operator will make false.
!(A && B) is t rue.
Assignment Operators
 We perform an operation on a variable, and then store the
result back into that variable
 Java provides assignment operators to simplify that process
 For example, the statement
num += count;
is equivalent to
num = num + count;
Assignment Operators
 There are many assignment operators in Java,
including the following:
Operator
+=
-=
*=
/=
%=
Example
x += y
x -= y
x *= y
x /= y
x %= y
Equivalent To
x = x + y
x = x - y
x = x * y
x = x / y
x = x % y
Assignment Operators
 The right hand side of an assignment operator can be a
complex expression
 The entire right-hand expression is evaluated first,
then the result is combined with the original variable
 Therefore
result /= (total-MIN) % num;
is equivalent to
result = result / ((total-MIN) % num);
Assignment Operators
 The behavior of some assignment operators depends
on the types of the operands
 If the operands to the += operator are strings, the
assignment operator performs string concatenation
 The behavior of an assignment operator (+=) is always
consistent with the behavior of the corresponding
operator (+)
Conditional Operator ( ? : )
 Conditional operator is also known as the ternary
operator. This operator consists of three operands and
is used to evaluate Boolean expressions. The goal of
the operator is to decide which value should be
assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
2-15/30
Expression
2-16/30
Expressions
 An expression is a combination of one or more operators
and operands
 Arithmetic expressions compute numeric results and
make use of the arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
• If either or both operands used by an arithmetic
operator are floating point, then the result is a
floating point
Division and Remainder
 If both operands to the division operator (/) are
integers, the result is an integer (the fractional part
is discarded)
• The remainder operator (%) returns the remainder
after dividing the second operand into the first
14 / 3 equals
8 / 12 equals
4
0
14 % 3 equals
8 % 12 equals
2
8
Operator Precedence
 Operators can be combined into complex
expressions
result = total + count / max - offset;
 Operators have a well-defined precedence which
determines the order in which they are evaluated
 Multiplication, division, and remainder are
evaluated prior to addition, subtraction, and string
concatenation
 Arithmetic operators with the same precedence are
evaluated from left to right, but parentheses can be
used to force the evaluation order
Operator Precedence
 What is the order of evaluation in the following
expressions?
a + b + c + d + e
1 432
a + b * c - d / e
3 241
a / (b + c) - d % e
2 341
a / (b * (c + (d - e)))
4 123
Assignment Revisited
 The assignment operator has a lower precedence
than the arithmetic operators
First the expression on the right hand
side of the = operator is evaluated
Then the result is stored in the
variable on the left hand side
answer = sum / 4 + MAX * lowest;
14 3 2
Then the result is stored in the variable on the left hand side
 The right and left hand sides of an assignment
statement can contain the same variable
First, one is added to the
original value of count
Then the result is stored back into count
(overwriting the original value)
count = count + 1;
KNOW THE OPERATOR PRECEDENCE TABLE
ON PAGE 78. It will grow significantly!

Opeartor &amp; expression

  • 1.
    P. Raveena Lakshmi,M.Sc., M.Phil., Assistant Professor
  • 2.
  • 3.
    Types of Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:  Arithmetic Operators  Relational Operators  Increment & Decrement Operators  Bitwise Operators  Logical Operators  Assignment Operators  Conditional Opeartors 2-3/30
  • 4.
    Arithmetic Operators  Arithmeticoperators are used in mathematical expressions in the same way that they are used in algebra. Assume integer variable A holds 10 and variable B holds 20, then: Operator Description Example + Addition - Adds values on either side of the operator A + B will give 30 - Subt raction - Subt racts right hand operand from left hand operand A - B will give -10 * Multiplication - Multiplies values on either side of the operator A * B will give 200 / Division - Divides left hand operand by right hand operand B / A will give 2 % Modulus - Divides left hand operand by right hand operand and returns remainder B % A will give 0
  • 5.
    Relational Operators  Thereare following relational operators supported by Java language Assume variable A holds 10 and variable B holds 20, then: Operator Description Example == Checks if the values of two operands are equal or not, if yes then condition becomes t rue. (A == B) is not t rue. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes t rue. (A != B) is t rue. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes t rue. (A > B) is not t rue.
  • 6.
    Operator Description Example <Checks if the value of left operand is less than the value of right operand, if yes then condition becomes t rue. (A < B) is t rue. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes t rue. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes t rue. (A <= B) is t rue. 2-6/30
  • 7.
    Increment and Decrement The increment and decrement operators use only one operand  The increment operator (++) adds one to its operand  The decrement operator (--) subtracts one from its operand  The statement count++; is functionally equivalent to count = count + 1;
  • 8.
    Increment and Decrement The increment and decrement operators can be applied in postfix form: count++  or prefix form: ++count
  • 9.
    Bitwise Operators  Javadefines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.  Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows: a = 0011 1100 b = 0000 1101 a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011 2-9/30
  • 10.
    Logical Operators  Thefollowing table lists the logical operators: Assume Boolean variables A holds t rue and variable B holds false, then: Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then the condition becomes t rue. (A && B) is false. || Called Logical OR Operator. If any of the two operands are nonzero, then the condition becomes t rue. (A || B) is t rue. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is t rue then Logical NOT operator will make false. !(A && B) is t rue.
  • 11.
    Assignment Operators  Weperform an operation on a variable, and then store the result back into that variable  Java provides assignment operators to simplify that process  For example, the statement num += count; is equivalent to num = num + count;
  • 12.
    Assignment Operators  Thereare many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y
  • 13.
    Assignment Operators  Theright hand side of an assignment operator can be a complex expression  The entire right-hand expression is evaluated first, then the result is combined with the original variable  Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);
  • 14.
    Assignment Operators  Thebehavior of some assignment operators depends on the types of the operands  If the operands to the += operator are strings, the assignment operator performs string concatenation  The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding operator (+)
  • 15.
    Conditional Operator (? : )  Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as: variable x = (expression) ? value if true : value if false 2-15/30
  • 16.
  • 17.
    Expressions  An expressionis a combination of one or more operators and operands  Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % • If either or both operands used by an arithmetic operator are floating point, then the result is a floating point
  • 18.
    Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) • The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals 4 0 14 % 3 equals 8 % 12 equals 2 8
  • 19.
    Operator Precedence  Operatorscan be combined into complex expressions result = total + count / max - offset;  Operators have a well-defined precedence which determines the order in which they are evaluated  Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation  Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order
  • 20.
    Operator Precedence  Whatis the order of evaluation in the following expressions? a + b + c + d + e 1 432 a + b * c - d / e 3 241 a / (b + c) - d % e 2 341 a / (b * (c + (d - e))) 4 123
  • 21.
    Assignment Revisited  Theassignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 14 3 2 Then the result is stored in the variable on the left hand side
  • 22.
     The rightand left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1; KNOW THE OPERATOR PRECEDENCE TABLE ON PAGE 78. It will grow significantly!