Sunday, 29 January 2012

Operators in Java

Introduction:
•An operator is used to perform some operation on operand(s).
•In Java all the operators can be classified into the following:
1) Arithmetic operators
2) Bitwise operators
3) Relational operators
4) Logical operators
5) Other operators


 
Short circuit logical operators:
•The short circuit logical operators are && and ||.
•The difference between the short circuit logical operators and the logical operators & and | is, if we use && and ||, in expressions, they will check the left sub-expression and if the result is false, then they will skip the right sub-expression.
•But if you use & and |, they will check both the left and right sub-expressions.


Assignment operator:
•Assignment operator is denoted as =
•The operand on the left side of the assignment operator (=) must be of the same type as the type of the result on the right side of assignment operator (=).

?: operator:
•Java provides a ternary (three-way) operator, which acts as an if-then-else statement.
•Syntax of this ternary operator (?:) is like this:

(expression1) ? (expression 2) : (expression 3)

expression 1 is evaluated first and if it is true, expression 2 is executed or if expression 1 is false, then expression 3 is executed.

No comments:

Post a Comment