Java Operators - The Coding Shala
Home >> Learn Java >> Java Operators
In Java, operators are used for the evaluation of expressions. There are many types of operators available in Java-like arithmetic, bitwise, relational, logical, assignment operators, etc.
To calculate mathematical expression we use arithmetic operators. The following image shows the Java arithmetic operators:
These operators are used on numeric operands and char types in Java. We cannot use them on boolean.
The modulus operator, % returns the remainder of a division operation. In Java, Increment (++) and Decrement (--) operators can be used as prefix and postfix. In the Prefix form, the operand is incremented or decremented before the value is obtained for the use in the expression. In the postfix form, the previous value is used in the expression, and then the operand is modified. For example:
The following Java Program explains the Arithmetic Operators:
Note: Here, '=' is an assignment operator.
Java Bitwise Operators
These bitwise operators work on the individual bits of their operands and can be applied to the integer types, int, long, short, byte, and char. The Java bitwise operators are shown below:
Note: All these operators can be used with assignment operators.
The following image shows the outcome of these bitwise logical operators:
The left shift operator(<<) shifts all of the bits of the value to the left by a specified number of times. Every time we shift to left, the high order bit(left bit) is shifted out(lost) and a zero(0) is added to the right.
The Right shift(>>), shifts all of the bits of the value to the right by a specified number of times. for every time we shift a bit to right, the top bit(leftmost) is filled in with the previous contents of the top bit. This is called sign extension and the rightmost bit is lost.
Note: The unsigned shift right operator (>>>) always shifts zeros into the high-order bit.
Java Relational Operators
The relational operators are used to determine the relationship between the operands. The relational operators are shown below:
The outcome of these operations is a boolean value. These operators are often used with if and loop statements.
Java Boolean Logical Operators
These boolean logical operators are used with only boolean operands. The boolean logical operators are shown below:
These operators work on boolean values in the same way that they work on the bits of an integer.
Other Posts You May Like