Start of Tutorial > Start of Trail > Start of Lesson | Search |
An operator performs a function on either one, two, or three operands. An operators that requires one operand is called a unary operator. For example,++
is a unary operator that increments the value of its operand by 1. An operator that requires two operands is a binary operators. For example,=
is a binary operator that assigns the value from its right-hand operand to its left-hand operand. And finally a ternary operator is one that requires three operands. The Java programming language has one ternary operator,?:
, which is a short-handif
-else
statement.The unary operators support either prefix or postfix notation. Prefix notation means that the operator appears before its operand:
Postfix notation means that the operator appears after its operand:operator opAll of the binary operators use infix notation, which means that the operator appears between its operands:op operatorThe ternary operator is also infix; each component of the operator appears between operands:op1 operator op2In addition to performing the operation, an operator also returns a value. The return value and its type depends on the operator and the type of its operands. For example, the arithmetic operators, which perform basic arithmetic operations such as addition and subtraction, return numbers--the result of the arithmetic operation. The data type returned by the arithmetic operators depends on the type of its operands: If you add two integers, you get an integer back. An operation is said to evaluate to its result.expr ? op1 : op2It's useful to divide the operators into these categories:
Start of Tutorial > Start of Trail > Start of Lesson | Search |