google Ads

Friday, July 17, 2009

Operators in JAVA

Operators


Operators perform some function on either one or two operands or three operands. Operators that require one operand are called unary operators. For example, ++ is a unary operator that increments the value of its operand by 1. Operators that require two operands are binary operators. For example, = is a binary operator that assigns the value from its right-hand operand to its left-hand operand. And finally ternary operators are those that require three operands. The Java language has one tertiary operator,?:, which is a short-hand if-else statement.
Java’s unary operators can use 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 Java’s binary operators use infix notation, which means that the operator appears between its operands:

op1 operator op2

Java’s only 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 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 is useful to divide Java’s operators into these categories: arithmetic, relational and conditional, bitwise and logical, and assignment.

These are discussed in the following sections.

Arithmetic Operators
Relational Operators
Logical operators
Assignment Operators
Increment and decrement Operators
Conditional Operators
Bitwise Operators

No comments:

Post a Comment