The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

## Operators

> 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-hand `if`-`else` statement.

> The unary operators support either prefix or postfix notation. Prefix notation means that the operator appears before its operand:

> >     operator op

> Postfix notation means that the operator appears after its operand:

> >     op operator

> All of the binary operators use infix notation, which means that the operator appears between its operands:

> >     op1 operator op2

> The ternary operator is also infix; each component of the operator appears between operands:

> >     expr ? op1 : op2

> In 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.

> It's useful to divide the operators into these categories:

> - Arithmetic Operators
> - Relational and Conditional Operators
> - Shift and Logical Operators
> - Assignment Operators
> - Ternary Operator

| Start of Tutorial > Start of Trail > Start of Lesson | Search