# Operators in Java7 min read

Operators in Java are used to perform operations on variables and values.

Some of the operators supported by Java are listed below.

## Logical Operators

• Assume Boolean variables A holds true and variable B holds false.
• Logical AND( && ): If both the operands are non-zero, then the condition becomes true (A && B) is false.
• Logical OR(II): If any of the two operands are non-zero, then the condition becomes true (A || B) is true.
• Logical NOT( ! ): Its operand’s logical state is reversed when it is used. The Logical NOT operator will produce false if a condition is true! (A && B) are both correct.

## Relational Operators

Assume variable A holds 10 and variable B holds 20

• Assignment( == ) Checks if the values of two operands are equal or not, if yes then the condition becomes true. In our example, (A == B) is not true.
• != ( Not Equal To ) Checks whether the values of two operands are equal or not; if they aren’t, the condition becomes true (A!= B).
• Greater Than( >) determines whether the left operand’s value is greater than the right operand’s value; if so, the condition is true (A > B).
• Less Than( < ) Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true
• Greater Than Equal Tool >= ) Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true (A >= B) is not true.
• Less Than Equal Tool <= ) Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true. (A <= B) is true.

## Conditional Operator

• The conditional operator is also known as the ternary operator.
• This operator consists of three operands and is used to evaluate Boolean expressions.
• The goal of the operator is to decide which value should be assigned to the variable.
• The operator is written as: variable x = (expression)? value if true : value if false

## Bitwise Operators

Java defines several bit-wise operators, which can be applied to the integer types, long, int, short, char, and byte

• The Bitwise operator works on bits and performs the bit-by-bit operation. Assume if a = 60; and b = 13; now in the binary format, they will be as follows:
• a = 0011 1100
• b = 0000 1101
• AND(&) Binary AND Operator copies a bit to the result if it exists in both operands (A & B) will give 12 which is 0000 1100.
• OR(I Binary OR Operator copies a bit if it exists in either operand (A | B) will give 61 which is 0011 1101.
• Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
• The Bitwise operator works on bits and performs the bit-by-bit operation. Assume if a = 60; and b = 13; now in the binary format, they will be as follows:
• a = 0011 1100
• b = 0000 1101
• AND(&) – Binary AND Operator copy a bit to the result if it exists in both operands (A & B) will give 12 which is 0000 1100.
• OR(||) – Binary OR Operator copies a bit if it exists in either operand (A | B) will give 61 which is 0011 1101.
• XOR(^) Binary XOR Operator copies the bit if it is set in one operand but not both (A^B) will give 49 which is 0011 0001 Ones Complement(-) Binary One’s Complement Operator is unary and has the effect of ‘flipping’ bits (~A) will give -61 which is 1100 0011 in 2’s complement form due to a signed binary number.
• Left Shift( << ) Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand A << 2 will give 240 which is 1111 0000.
• Right Shift( >>) Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand A >> 2 will give 15 which is 1111.
• Shift right zero-fill( >>>) The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros A >>>2 will give 15 which is 0000 1111.

## Assignment Operators

There are the following assignment operators supported by the Java language:

• Simple assignment operator( = ): Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
• Add AND assignment operator( += ): It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
• Subtract AND assignment operator( -= ): It subtracts the right operand from the left operand and assigns the result to left operand C-= A is equivalent to C = C-A
• Multiply AND assignment operator( *= ): It multiplies right operand with the left operand and assigns the result to left operand C*= A is equivalent to C = C* A
• Divide AND assignment operator( /= ): It divides left operand with the right operand and assigns the result to left operand C/= A is equivalent to C = C/A
• Modulus AND assignment operator( %=): It takes modulus using two operands and assign the result to left operand C%= A is equivalent to C = C % A

## Arithmetic Operators

Assume integer variable A holds 10 and variable B holds 20, then:

• Addition ( + ) Adds values on either side of the operator
• A + B will give 30
• Subtraction ( – ) Subtracts right-hand operand from left-hand operand
• A – B will give -10
• Multiplication (*) Multiplies values on either side of the operator
• A * B will give 200
• Division ( / ) Divides left hand operand by right hand operand
• B / A will give 2
• Modulus ( % ) Divides left-hand operand by right-hand operand and returns the remainder
• B % A will give 0
• Increment ( ++ ) Increases the value of operand by 1
• B++ gives 21
• Decrement ( — ) Decreases the value of operand by 1
• B– gives 19

## Operator Precedence

• Generally, we use parenthesis to set the priority or order of evaluation. But in a complicated expression, it is not suitable.
• Operator precedence rules are very important in evaluating the expression if any expression contains several operators.
• The operator with the highest priority is evaluated first and the operators with the lowest priority are evaluated last.
• The list of operators in order from higher precedence to lowest precedence is given below.

Operators with the same priority have the same precedence. If any expression contains more operators with the same or different priority then unary operators and assignment operators are evaluated right-to-left, and the remaining operators are evaluated right-to-left.

Share: