Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

4 Operators


4 Operators


J


ava provides a rich operator environment. Most of its operators can be divided into the
following four groups: arithmetic, bitwise, relational, and logical. Java also defines some
additional operators that handle certain special situations. This chapter describes all
of Java’s operators except for the type comparison operatorinstanceof, which is examined
in Chapter 13.

Arithmetic Operators


Arithmetic operators are used in mathematical expressions in the same way that they are
used in algebra. The following table lists the arithmetic operators:

Operator Result
+ Addition


  • Subtraction (also unar y minus)



  • Multiplication
    / Division
    % Modulus
    ++ Increment
    += Addition assignment
    –= Subtraction assignment
    *= Multiplication assignment
    /= Division assignment
    %= Modulus assignment



  • – Decrement


The operands of the arithmetic operators must be of a numeric type. You cannot use
them onbooleantypes, but you can use them onchartypes, since thechartype in Java is,
essentially, a subset ofint.

57

Free download pdf