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

An operator performs a function on one, two, or three operands. An operator that requires one operand is called aunary operator. For example,`++`

is a unary operator that increments the value of its operand by 1. An operator that requires two operands is abinary operator. For example,`=`

is a binary operator that assigns the value from its right-hand operand to its left-hand operand. And finally, aternary operatoris 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 notationmeans that the operator appearsbeforeits operand:operator op //prefix notationPostfix notationmeans that the operator appearsafterits operand:All the binary operators useop operator //postfix notationinfix notation, which means that the operator appearsbetweenits operands:The ternary operator is also infix; each component of the operator appears between operands:op1 operator op2 //infix notationIn 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 toop1 ? op2 : op3 //infix notationevaluate toits result.We divide the operators into these categories:

We end this lesson with Summary of Operators and Questions and Exercises: Operators.

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

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