Basic Operators
Programming Java provides a rich set of operators to manipulate variables. Details Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: • Arithmetic Operators • Relational Operators • Bitwise Operators • Logical Operators • Assignment Operators • Misc OperatorsThe Arithmetic Operators:Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. + Addition - Adds values on either side of the operator - Subtraction - Subtracts right hand operand from left hand operand * Multiplication - Multiplies values on either side of the operator / Division - Divides left hand operand by right hand operand % Modulus - Divides left hand operand by right hand operand and returns remainder ++ Increment - Increases the value of operand by 1 -- Decrement - Decreases the value of operand by 1 The Relational Operators: There are following relational operators supported by Java language == Checks if the values of two operands are equal or not, if yes then condition becomes true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. The Bitwise Operators: Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation & Binary AND Operator copies a bit to the result if it exists in both operands. | Binary OR Operator copies a bit if it exists in either operand. ^ Binary XOR Operator copies the bit if it is set in one operand but not both. ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. The Logical Operators: The following table lists the logical operators: && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. || Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. The Assignment Operators: There are following assignment operators supported by Java language: = Simple assignment operator, Assigns values from right side operands to left side operand += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand <<= Left shift AND assignment operator >>= Right shift AND assignment operator &= Bitwise AND assignment operator ^= bitwise exclusive OR and assignment operator |= bitwise inclusive OR and assignment operator
Sent from Windows Mail