In any programming
language, real life scenario involves the manipulation of variables. Operators
specify the evaluation to be carried on data objects. Operators combine simple
value or expression into complex new expression that return values.
Type of java
operator,
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
- instanceOf operator
- Conditional operators
Arithmetic Operators
Arithmetic operators
are used in mathematical expressions. Operands of arithmetic operators must be
of numeric type. While using arithmetic operators, Boolean operands cannot be
used. Character operands are allowed.
Relational Operators
Relational operators
are used to test the relation between to operands. Expressions using relation
operators always return Boolean values, i.e. either true or false.
Logical Operators
Logical operator’s
works with Boolean operands.
Assignment operator
Assignment operator
is used to assign values. The assignment operator is a single equal sign “-”
and assigns a value to a variable. Assigning values to more than one variable
can be done. It can be also said as the chain of assignments.
Bitwise Operators
Bitwise operators
are used to manipulate individual bits in an integral primitive data type.
Bitwise operators act upon the individual bits of their operands. Bitwise
operators perform Boolean algebra on the corresponding bits in the two
arguments to produce result. When bitwise operators are used with numbers the
“&” operation performs the bitwise “AND” function, and the “|” operations
performs the bitwise “OR” function on each pair of bits.
instanceOf operator
The “instanceOf”
operator is used for object reference variables only. It can also be used to
check weather an object is of a particular type.
Example:
Scanner
user_input=new Scanner(System.in);
Boolean
value=input_user instanceOf Scanner;
System.out.println(value);
Conditional Operator
Conditional operator
is used to evaluate Boolean expressions. The ternary operator is a conditional
operator. Conditional operator can replace certain type of if-else statements.
Syntax:
expression1 ?
expression2 : expression3
The “expression1”
can be any expression that evaluates to a Boolean value. If it returns a true
value “expression2” is evaluated and if it returns a false value “expression3”
is evaluated. We can even nest the conditional operators.
No comments:
Post a Comment