Arithmetic
Operators
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
Increment ++
Addition
Assignment +=
Subtraction
Assignment -=
Multiplication
Assignment *=
Division
Assignment /=
Modulus
Assignment %=
Decrement --
Arithmetic
Operators
üIf either or both operands associated
with an arithmetic operator
are floating point, the result is a floating point.
ü% operator applies both to floating-point
type and integer types.
üExample:
class
modulus
{
public static void main (String args [])
{
int x = 42;
double y = 42.3;
System.out.println(“x mod 10 =“ + x%10);
System.out.println(“y mod 10 = “ + y%10);
}
}
Output:
x mod
10 =2
y mod
10 = 2.3
Increment
and Decrement
üThe increment and decrement
operators are
arithmetic and operate on 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 prefix form (before the operand) or
postfix
form
(after the operand)
üWhen used alone in a statement, the
prefix and
postfix forms are functionally equivalent. That is,
count++;
is equivalent to ++count;
Increment
and Decrement
üWhen used in a larger expression, the
prefix and
postfix forms have different effects
üIn both cases the variable is incremented
(decremented)
üBut the value used in the larger
expression depends on
the form used.
üIf count currently contains 45, then the
statement total
= count++;
assigns 45 to total and 46 to count
üIf count currently contains 45, then the
statement total
= ++count;
assigns the value 46 to both total and count
No comments:
Post a Comment