OPERATORS
Operator:
It is a symbol that is used to
represent a particular operation.
Operands:
The value given to the operation.
Based
on the number of operands operators has three types:
2. Binary Operator.
3. Ternary Operator.
I.
Unary Operator:-
It requires only one operand.
Ex. Increment, Decrement
operator, Logical NOT operator.
II. Binary Operator:-
It requires two operands.
Ex. Arithmetic operator,
Relational operator, Logical AND operator, Logical OR operator.
III.
Ternary Operator:-
It requires three operands.
Ex. Conditional operator.
Binary Operator:-
It
requires two operands.
Ex.
Arithmetic operator, Relational operator, Logical AND operator, Logical OR
operator .
a) Arithmetic
Operators:-
Arithmetic Operators |
|
Operators |
Operation |
+ |
Addition |
- |
Substraction |
* |
Multiplication |
/ |
Division (Quotent) |
% |
Modulus
(Remainder) |
b)
Relational
Operators:-
Relational Operators |
|
Operators |
Operation |
> |
Greater
than |
> = |
Greater than equal to |
< |
Less
than |
< = |
Less than equal to |
= = |
Equal
to |
! = |
Not equal to |
Program
- public class Maths
- {
- public static void main(String [] args)
- {
- int a=23;
- int b=22;
- System.out.print(a!=b);
- }
- }
Output:
c)
Logical AND
Operator (&&)
·
If both the
operands conditions are true then only result will be true.
·
If anyone
operand is false the result will be false.
Logical AND Operator |
||
Operand 1 |
Operand 2 |
Result |
T |
T |
T |
T |
F |
F |
F |
T |
F |
F |
F |
F |
Program
- class Maths
- {
- public static void main(String [] args)
- {
- int a=23;
- int b=22;
- System.out.print(a!=b && a>b);
- }
- }
Output:
d)
Logical OR
operator ( | | )
· If any one condition is true then the result will be
true.
Logical OR Operator |
||
Operand 1 |
Operand 2 |
Result |
T |
T |
T |
T |
F |
T |
F |
T |
T |
F |
F |
F |
Program
- class Maths
- {
- public static void main(String [] args)
- {
- int a=23;
- int b=22;
- System.out.print(a=b | | a>b);
- }
- }
Output:
True
- Logic to check whether the number is even or not, n = 4
- Logic to check
whether the number is odd or not, n = 27
Ternary Operator:-
·
It
requires three operands.
·
Ex.
Conditional operator.
a.
Conditional
Operator
·
It is an
example of ternary operator.
·
In conditional
operator we will be having three operands.
·
In operand 1 we
write the condition, If the condition is true the operand 2 will get called and
if the condition is false the operand 3 will get called.
Syntax
Program
- class Maths
- {
- public static void main(String [] args)
- {
- int a=23;
- int b=22;
- System.out.print(a < b ? a : b);
- }
- }
Output:
22
1. //Write a java program to check whether the number is even or odd and print by using conditional operator. n = 1044
- class A
- {
- public static void main(String [] args)
- {
- int a=1044;
- int b=2;
- System.out.println(a % b==0 ? “Even” : “Odd”);
- }
- }
Output:
Even
----------------xxxxxxxxx-------------------------xxxxxxxx-------------------------xxxxxxxx---------------------------
- class A
- {
- public static void main(String [] args)
- {
- int a=1044;
- int b=2;
- String x="even";
- String y="odd";
- System.out.println(a % b==0 ? x : y);
- }
- }
Output:
Even
0 Comments