Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. In this tutorial , we will try to cover the most commonly used operators in programming.
First, let's categorize them:
|+||addition||x+y||Adds values on either side of the operator|
|-||subtraction||x-y||Subtracts the right hand operand from the left hand operand|
|*||multiplication||x*y||Multiplies values on either side of the operator|
|/||division||x/y||Divides the left hand operand by the right hand operand|
|%||modulus||x%y||Divides the left hand operand by the right hand operand and returns remainder|
Relational Operators: These operators are used for comparison. They return either true or false based on the comparison result. The operator '==' should not be confused with '='. The relational operators are as follows:
|==||equal||x == y||Checks if the values of two operands are equal or not, if yes then condition becomes true.|
|!=||not equal||x != y||Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.|
|>||greater than||x > y||Checks if the value of the left operand is greater than the value of the right operand, if yes then condition becomes true|
|<||less than||x < y||Checks if the value of the left operand is less than the value of the right operand, if yes then condition becomes true.|
|>=||greater than or equal||x >= y||Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then condition becomes true.|
|<=||less than or equal||x <= y||Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then condition becomes true.|
Bitwise Operators: These operators are very useful and we have some tricks based on these operators. These operators convert the given integers into binary and then perform the required operation, and give back the result in decimal representation.
|&||bitwise AND||x & y||Sets the bit to the result if it is set in both operands.|
||||bitwise OR||x | y||Sets the bit to the result if it is set in either operand.|
|^||bitwise XOR||x ^ y||Sets the bit if it is set in one operand but not both|
|~||bitwise NOT||~x||Unary operator and has the effect of 'flipping' bits,i.e, flips 1 to 0 and 0 to 1.|
|<<||left shift||x << y||The left operand's value is moved left by the number of bits specified by the right operand. It is equivalent to multiplying x by $$2^y$$|
|>>||right shift||x >> y||The left operand's value is moved right by the number of bits specified by the right operand.It is equivalent to dividing x by $$2^y$$|
Assume x=42, y=27
x&y = 0000 1010= 10 (in decimal)
$$x|y$$ = 0011 1011= 59
$$x$$ ^ $$y$$ = 0011 0001= 49
~$$x$$ = 1101 0101
$$x<<2$$ = 1010 1000= 168. Notice, the bits are shifted 2 units to the left and the new bits are filled by 0s.
$$x>>2$$ = 0000 1010=10$$. Notice, the bits are shifted 2 units to the right and the new bits are filled by 0s.
For more information about how these operators work, see : Bit Manipulation
Logical Operators: These operators take boolean values as input and return boolean values as output.
Note: In C,C++ any non-zero number is treated as true and 0 as false but this doesn't hold for Java.
|&&||logical AND||x && y||Returns true if both x and y are true else returns false.|
|||||logical OR||x || y||Returns false if neither x nor y is true else returns true|
|!||logical NOT||! x||Unary operator. Returns true if x is false else returns false.|
|=||assignment||x = y||Assigns value from the right side operand(s) to the left side operand.|
|+=||add and assignment||x += y||x = x+y||Adds the right side operand to the left side operand and assigns the result to the left side operand.|
|-=||subtract and assignment||x -= y||x= x-y||Subtracts the right side operand from the left side operand and assigns the result to the left side operand.|
|*=||multiply and assignment||x *= y||x= x*y||Multiplies the right side operand with the left side operand and assigns the result to the left side operand.|
|/=||divide and assignment||x /= y||x= x/y||Divides the left side operand with the right side operand and assigns the result to the left side operand.|
|%=||modulus and assignment||x%=y||x= x%y||Takes modulus using the two operands and assigns the result to the left side operand.|
|<<=||left shift and assignment||x<<=y||x= x<< y||Shifts the value of x by y bits towards the left and stores the result back in x.|
|>>=||right shift and assignment||x>>=y||x= x>>y||Shifts the value of x by y bits towards the right and stores the result back in x.|
|&=||bitwise AND and assignment||x&=y||x= x&y||Does x&y and stores result back in x.|
||=||bitwise OR and assignment||x|=y||x= x|y||Does x|y and stores result back in x|
|^=||bitwise XOR and assignment||x^=y||x= x^y||Does x^y and stores result back in x.|
Increment/Decrement Operators: These are unary operators. Unary operators are the operators which require only one operand.
|++||Postincrement||x++||Increment x by 1 after using its value|
|--||Postdecrement||x--||Decrement x by 1 after using its value|
|++||Preincrement||++x||Increment x by 1 before using its value|
|--||Predecrement||--x||Decrement x by 1 before using its value|
then, after y=x++; y=10 and x=11, this is because x is assigned to y before its increment.
but if we had written y=++x; y=11 and x=11, because x is assigned to y after its increment.
Same holds for decrement operators.
Conditional Operator: It is similar to if-else:
x = (condition) ? a : b
If condition is true,then a is assigned to x else b is assigned to x. It is a ternary operator because it uses the condition, a and b i.e. three operands (the condition is also treated as a boolean operand).
Operator Precedence and Associativity:
Precedence Rules: The precedence rules specify which operator is evaluated first when two operators with different precedence are adjacent in an expression.
For example: $$x= a+++b$$
This expression can be seen as postfix increment on a and addition with b or prefix increment on b and addtion to a. Such issues are resolved by using precedence rules.
Associativity Rules: The associativity rules specify which operator is evaluated first when two operators with the same precedence are adjacent in an expression.
For example: $$a*b /c$$
Operator Precedence: The following table describes the precedence order of the operators mentioned above. Here, the operators with the highest precedence appear at the top and those with the lowest at the bottom. In any given expression, the operators with higher precedence will be evaluated first.
LR= Left to Right
RL=Right to Left
|Unary||RL||+ - ! ~ ++ --|
|Multiplicative||LR||* / %|
|Relational||LR||< <= > >=|
|Assignment||RL||= += -= *= /= %= >>= <<= &= ^= |=|