Operators in Java with example
Operators in Java:
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. It is used in programs to manipulate data and variables. It supports a rich set of operators. Java operators can be classified into 8 categories:
Arithmetic operators: These Operators are used to construct mathematical expressions as in algebra. Java provides all the basic arithmetic operators.
Operator | Meaning |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder |
Relational operators:
When we compare two quantities and depending on their relation, then we use some relational symbols that say called Relational Operators. It has the following syntax:
variable-name relation-operator value;
Operator | Meaning |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
(==) | equal to |
!= | not equal to |
Logical Operators:
An expression that combines two or more relational expressions is called Logical Operators or Compound Operators.
Operator | Meaning |
&& | logical AND |
|| | logical OR |
! | logical NOT |
Assignment Operators:
These Operators are used to assign the value of an expression to a variable. It has the following syntax:
variable = value;
Increment and decrement operators:
Java has two useful operators and these are the Increment and decrement operators. It has the following syntax:
variable+ + // increment operator
and
variable-- // decrement operator
Example of Increment Operator:
i = i + 1; same as ++i; or i++; // increment operator
Example of Decrement Operator:
i= i - 1; same --i; or i-- // decrement operator
Conditional Operators:
The character pair ?: is a ternary operator. It is used to construct conditional expressions that are called Conditional Operators. It has the following syntax:
expression1 ? expression2 : expression3;
Special Operators:
Java supports some special operators such as instance of operator and member of selection operator or dot operator (.). It has the following syntax:
obj1 instanceof obj2;
Bitwise Operators:
Java has a distinction of supporting special operators that are known as Bitwise Operators. It is used to manipulate data at values of bit-level and is also used for testing the bits or shifting them to the right or left. Bitwise Operators may not be applied to float or double data types.
Operator | Meaning |
& | bitwise AND |
! | bitwise OR |
^ | bitwise exclusive OR |
~ | One's Complement |
<< | Shift left |
>> | Shift right |
>>> | Shift right with zero fill |