The JavaTM Tutorial

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

Trail: Learning the Java Language
Lesson: Language Basics

## Operators

> An operator performs a function on one, two, or three operands. An operator 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 operator . 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             //prefix notation

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

> >     op operator             //postfix notation

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

> >     op1 operator op2        //infix notation

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

> >     op1 ? op2 : op3         //infix notation

> In addition to performing the operation, an operator returns a value. The return value and its type depend 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 an arithmetic operator 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.

> We divide the operators into these categories:

> - Arithmetic Operators
> - Relational and Conditional Operators
> - Shift and Logical Operators
> - Assignment Operators
> - Other Operators
> - Summary of Operators
> - Questions and Exercises: Operators

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

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.