An operator is a combination of characters that identifies a data operation, such as addition. Operators transform one or more operands, data items that serve as arguments to an operator, into a new data item. Operators evaluate the operands according to a predefined evaluation order. For example, the subtraction operator subtracts its rightmost operand from its leftmost operand.
The number of required operands classify an operator. The resulting classifications are unary, binary, and ternary.
A unary operator is an operator that takes one operand; negation is an example. If an operator takes two operands, it is a binary operator. Examples of binary operators include multiplication and division. Finally, a ternary operator is an operator that takes three operands. Java's only ternary operator is the conditional operator.
The compiler ensures, via widening and narrowing conversion rules, that the data types of a binary or ternary operator's operands match. For example, if one operand's data type to the division operator is integer, and the other operand's data type is short integer, the compiler will generate code that converts the short integer to an integer prior to generating code that performs the division.
The position of an operator relative to its operand(s) classifies the operator. These classifications are prefix, postfix, and infix.
A prefix operator is an operator that precedes its operand, whereas a postfix operator trails its operand. An example of a prefix operator is bit-wise complement; an example of a postfix operator is postincrement. All unary operators are either prefix or postfix operators.
An infix operator is situated between its operands. An example of an infix operator is the addition operator, as the plus sign appears between its operands. All binary and ternary operators are infix operators.
The assignment binary infix operator is the simplest of all operators. It assigns its rightmost operand to its leftmost operand, which must be a variable. The syntax of the operator is:
variable_name '=' operand
Example:
int num = 27; // The assignment operator is used to initialize a variable. num = 32; // This operator is also used to assign a value to an existing variable.
Java provides 11 arithmetic operators: addition, subtraction, multiplication, division, modulus, preincrement, postincrement, predecrement, postdecrement, negation, and unary plus.