Expression
The
Expression is the valid combination of operators and operands. Operators are
one which perform the operation and the operands are one on which the operation
is performed.
e..g.
3*4+5
is an expression .
Operators
are * and + , and Operands are 3,4 and 5.
Operators in Java:
·
Arithmetic Operators
·
Relational Operators
·
Logical Operators
·
Assignment Operators
·
Conditional Operator
·
Increment and Decrement Operator
·
Bitwise Operators
·
Special Operators
Priority Of Operators
{}
()
!
*,/,%
+,-
>,<,>=,<=
==,!=
&&
||
=
(i) Arithmetic Operators:
These Operators are used to perform the calculation
Java has five arithmetic operators:
Operator
Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
The behaviour of the +,-,*,/ operator is same e as c,c++. The modulus operator can
be applied to integer as well as floating-point type while in case of c/c++ it
can be applied to integer type only.
Important Rule Regarding calculating the Modulus:
To
calculate the modulus, ignore the sign and calculate the remainder and the sign
of the resultant value will depends on the sign of the left operand in the
expression.
Example:
int
i=5%2;
int
j=-5%2;
int
k=5%-2;
int
m=-5%-2;
Answer:
i=1
j=-1
k=1
m=-1
(ii) Relational Operators:
The
Relational operators are used for comparing the two values and the result of
the comparison is a Boolean value i.e. true or false.
Java supports
the following ordinal comparisons /
relational operators.
Operator Description > Greater Than
< Less
Than
>= Greater
than or equal to
<= Less
than or equal to
== Equal
to
!= Not
equal to
These operators are used to compare ordinal data
types. Data types where values have numeric order.
(iii) Logical Operators:
There
are three types of Logical Operators:
(a)
Logical AND(&&)
(b)
Logical OR(||)
(c)
Logical NOT(!)
(a) Logical AND (&&)
The
Logical AND (&&) operators is used to combine two conditions and the
result of the operation is true if and only if both of the conditions are true.
The truth table of the Logical AND
Condition1
Condition2 Condition1&&Condition2
false false false
false true false
true false false
true true true
It is
also known as the Short Circuit Logical AND Operator because if the first
condition hold false it will not check the second condition.
(b) Logical OR(||)
The
Logical OR (||) operators are used to combine two conditions and the result of
the operation is true if any of the one or both conditions are true.
The truth table of the Logical OR
Condition1
Condition2 Condition1||Condition2
false false false
false true true
true false true
true true true
It is
also known as the Short Circuit Logical OR Operator because if the first
condition hold true it will not check the second condition.
(c) Logical NOT(!)
This
operator is used to reverse the condition and if the condition is true then the
condition will become false and if the condition is false then the condition
will become true.
The truth table of the Logical NOT(!)
Condition !Condition
false true
true false
(iv) Assignment Operator:
The =
operators is known as the Assignment Operators and it is used to assign the
value into the variable.
Syntax: <Variable> = <expression>
Example:
a=2
It
will assign the value 2 into the variable a
a==2
It
will compare whether the value stored in the variable a is equal to 2 or not.
(v) Increment and Decrement Operators:
++ Operator is known as Increment Operator and it is
used to increase the value of variable by 1.
There are two types of Increment Operators:
(i)
Pre-Increment Operator
(ii)
Post-Increment Operator
(i) Pre-Increment Operator
In
this case, first the increment operation is performed and then the other
operation will take place.
Consider
the following example:
i=6;
j=++i;
So,
the above statement is equivalent to ,
i=i+1==>
i=6+1=7
j=i; ==> j=7
(ii)
Post-Increment Operator
In
this case, first the other operation is performed, and then the increment will
take place.
Consider the following example:
i=6
j=i++;
The above mentioned statement is equivalent to
j=i;
==> j=6
i=i+1
==> i=6+1==> 7
Consider the following code,
int
a=4;
int
b,c;
b=2*(a++);
c=2*(++a);
Find out the final value of a, b, and c?
a=6 ,
b=8 , c=12
y=2;
y=++y*y++*++y;
Decrement Operators:
(i)
Pre- Decrement Operators:
(ii)
Post- Decrement Operators:
(vi) Bitwise Operators:
Java's bitwise operators operate
on individual bits of integer (int and long) values. If an operand is shorter
than an int, it is promoted to int before doing the operations.
The
bitwise operators operates on the individuals bits of the numbers. To perform
the operators, we have to
convert them into their binary equivalents.
List
of the Bitwise Operators:
(i)
One's Complement
(ii)
Bitwise AND(&)
(iii)
Bitwise OR(|)
(iv)
Bitwise XOR(^)
(v)
Bitwise Arithmetic Right Shift (>>)
(vi)
Bitwise Unsigned Right Shift (>>>)
(vii)
Bitwise Left Shift
Comments
Post a Comment