Java Operators
Operator is a symbol which tells to the compiler to perform some operation. Java provides a rich set of operators do deal with various types of operations. Sometimes we need to perform arithmetic operations then we use plus (+) operator for addition, multiply(*) for multiplication etc.
Java operators can be divided into following categories:
Arithmetic operators:
Arithmetic operators are used to perform arithmetic operations like: addition, subtraction etc and helpful to solve mathematical expressions. The below table contains Arithmetic operators.
Operator | Description |
+ | adds two operands |
- | subtract second operands from first |
* | multiply two operand |
/ | divide numerator by denumerator |
% | remainder of division |
++ -- | Increment operator increases integer value by one Decrement operator decreases integer value by one |
Relation operators :
Relational operators are used to test comparison between operands or values. It can be use to test whether two values are equal or not equal or less than or greater than etc.
The following table shows all relation operators supported by Java.
Operator | Description |
== | Check if two operand are equal |
!= | Check if two operand are not equal. |
> | Check if operand on the left is greater than operand on the right |
< | Check operand on the left is smaller than right operand |
>= | check left operand is greater than or equal to right operand |
<= | Check if operand on left is smaller than or equal to right operand |
Logical operators :
Logical Operators are used to check conditional expression. For example, we can use logical operators in if statement to evaluate conditional based expression. We can use them into loop as well to evaluate a condition.
Java supports following 3 logical operator. Suppose we have two variables whose values are: a=true and b=false.
Operator | Description | Example |
&& | Logical AND | (a && b) is false |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is false |
Bitwise operators
Bitwise operators are used to perform operations bit by bit.
Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte.
The following table shows all bitwise operators supported by Java.
Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
<< | left shift |
>> | right shift |
Assignment Operators
Assignment operators are used to assign a value to a variable. It can also be used combine with arithmetic operators to perform arithmetic operations and then assign the result to the variable. Assignment operator supported by Java are as follows:
Operator | Description | Example |
= | assigns values from right side operands to left side operand | a = b |
+= | adds right operand to the left operand and assign the result to left | a+=b is same as a=a+b |
-= | subtracts right operand from the left operand and assign the result to left operand | a-=b is same as a=a-b |
*= | mutiply left operand with the right operand and assign the result to left operand | a*=b is same as a=a*b |
/= | divides left operand with the right operand and assign the result to left operand | a/=b is same as a=a/b |
%= | calculate modulus using two operands and assign the result to left operand | a%=b is same as a=a%b |
Powered by Froala Editor