Operators in java is a symbol that is used to perform mathematical , comparison, logical operations in java. java provides many operators to manipulate variables.
Categories of Operators in java
(1) Arithmetic Operators(2) Relation Operators
(3) Assignment Operators
(4) Logical Operators
(5) Bitwise Operators etc...
Arithmetic Operators
Assume variable A holds 10 values and variable B hods 20 value
(1) + (Addition)
A + B = 30; //add values on either sides of the operators
(2) - (Subtraction)
A - B = -10; //subtract right hand operand from left hand operand
(3) * (Multiplication)
A * B = 200; //multiplies values on either sides of the operators
(4) / (Division)
B / A = 2; //divides left hand operand by right hand operand
(5) % (Modulus)
B % A = 0; //divides left hand operand by right hand operand and returns remainder
(6) ++ (Increment)
A++ = 11; //increases the value of operand by 1
(7) -- (Decrement)
A-- = 9; //decreases the value of operand by 1
Operand here A and B is operand e.g A + B
A is operand
B is operand
+ is operator
Relational Operators
Relational operators return boolean or logical result(true or false)Assume variable A holds 10 values and variable B hods 20 value
(1) == (equal to)
(A == B) is not true. //Checks if the values of two operands are equal or not . if yes then condition becomes true.
(2) != ( not equal to)
(A !== B) is true. //Checks if the values of two operands are equal or not . if not then condition becomes true.
(3) >(greater than)
(A > B) is not true. Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
(3) <(less than)
(A < B) is true. Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(4) >= (greater than or equal to)
(A >= B) is not true. Checks if the value left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
3) <=(less than or equal to)
(A <= B) is true. Checks if the value of left operand is less than or equal to value of right operand, if yes then condition becomes true.
Assignment Operators
Assume there is three variables a =10, b=20, c(1) = (Simple Assignment Operator)
c = a + b; //assign a value of a + b into c variable 30
(2) += (Add AND Assignment Operator)
c += a; //c += a is equivalent to c = c + a
(3) -= (Subtract AND Assignment Operator)
c -= a; //c -= a is equivalent to c = c - a;
(4) *= ( Multiply AND Assignment Operator)
c *= a; //c *= a is equivalent to c = c * a;
(5) /= (Divide AND Assignment Operator)
c /= a; //c /= a is equivalent to c = c / a;
(6) %= (Modulus AND Assignment Operator)
c %= a; //c %= a is equivalent to c = c % a;
(7) <<= (Left Shift AND Assignment Operator)
c <<= 2; //c <<=2 is equivalent to c = c << 2
(8) >>= (Bitwise AND Assignment Operator)
c >>= 2; //c >>=2 is equivalent to c = c >> 2
Logical Operators
Assume boolean variable A hold true and B hold false value(1) && (logical AND Operator)
(A && B) is false //if both the operand A and B are non-zero then condition becomes true.
(2) || (logical OR Operator)
(A || B) is true //if any of the two operand A and B are non-zero then condition becomes true.
(3) ! (logical NOT Operator)
!(A && B) is true //use to reverses the logical state of it operand. if the condition is true logical NOT operator make it false.
0 comments:
Post a Comment