OPERATORS
Operators are the keywords in any programming language that perform the specific pre defined or user defined task on a set of operands . Operands are the values on which the operation is performed to result into a specific value .
Following types of operators are available in JAVA . Here is the categorization of Operators and their details with examples :
Arithmetic operators :
- Arithmetic operators as the name suggests perform the arithmetic operations .
- Arithmetic operations are performed on two given operations .
- Following Arithmetic operations are available in JAVA :
- Addition (+)
- Subtraction(-)
- Multiplication(*)
- Division(/)
- Modulus (%)
25%5=5
i.e. modulus operator between two operands gives us the result and would be equal to the remainder that we get after dividing both the operands.
Now, how you think a java program will resolve the following problem:
- (-10)%(3)
- (10)%(-3)
- (-10)%(-3)
Hence result to above three problems would be :
- (-10)%(3) = -1
- (10)%(-3) = 1
- (-10)%(-3) = -1
Logical Operators
These operators perform the logical tasks . We have three logical operatos and these are :
- AND operator (&&)
- OR operator (||)
- NOT operator (!)
AND operator considers the statement to be valid if both the two statements on which operator is applied are true . In other words we can say that , logic is true iff both A and B are true.
OR operator considers the statement to be valid if any one statement among the given is valid .
In other words logic is true if any one operand is true .
NOT operator compliments the logic . That is it changes true to false and vice versa.
for more clarification consider the table below :
A
|
B
|
A&&B
|
A||B
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
0
|
0
|
1
|
1
|
1
|
1
|
1
|
Conditional operators :We have only one conditional operator called TERNARY operators.
the operator is represented by ?: and it works like an if else statement .
consider the following statement
a>b?a:b
It means that "If a>b then a Else b"
Relational Operators :
Relational operators are basically used to compare two numbers.
in JAVA we have the following relational operators .
( > , < , >= , <= , == ) and these are (greater than , less than , greater than equl to , less than equal to , to check the equality) respectively .
Bit -Wise Operators :
Bit Wise operators are (&,|, ^, >> ,<<).
Now the point is that what is the difference between logical and Bit-Wise operators ?
Both of the operators perform AND , OR operation but the difference is in the way they perform .
in BIT-WISE operators the operands are first converted into the binary form i.e. bits and then the operation is performed .
Bit Wise operators are (&,|, ^, >> ,<<).
Now the point is that what is the difference between logical and Bit-Wise operators ?
Both of the operators perform AND , OR operation but the difference is in the way they perform .
in BIT-WISE operators the operands are first converted into the binary form i.e. bits and then the operation is performed .
Increment and Decrement Operators :
These operators are called UNARY OPERATORS as they operate on a single Operand.
Increment operator increments the number and operates in two ways :
++a and a++ :
a++
(Post
increment)
|
++a
(Pre
Increment)
|
Consider the following expression :
int a = 10 ;
int b = a++;
It will first assign the value of ‘a’
to ‘b’ and then will increment ‘a’.
The output of the above expression
would be :
a = 11
b = 10
|
Consider the following expression :
int a = 10 ;
int b = ++a ;
It will first increment the value of ‘a’
and then will assign the value to ‘b’.
The output of the above expression
would be :
a = 11
b = 11
|
Same is in the case of decrement operator . It decreases the value of operand by 1 .
In decrement operator also we have same same post decrement ans pre decrement and they work in similar way :
In decrement operator also we have same same post decrement ans pre decrement and they work in similar way :
a--
(Post
increment)
|
--a
(Pre
Increment)
|
Consider the following expression :
int a = 10 ;
int b = b-- ;
It will first assign the value of ‘a’
to ‘b’ and then will decrement ‘a’.
The output of the above expression
would be :
a = 9
b = 10
|
Consider the following expression :
int a = 10 ;
int b = --b ;
It will first decrement the value of ‘a’
and then will assign the value to ‘b’.
The output of the above expression
would be :
a = 9
b = 9
|
0 comments:
Post a Comment