Last Updated: 22 September, 2024
Operators are special symbols that perform the specified operations on one or more operands in Java. Each operator is responsible for performing some specific operation on variables, values, or expressions and returning a result.
Operators perform a variety of tasks in Java programs, including calculations, value comparisons, decision-making, and other actions.
Consider the expression 10 + 20 = 30, where 10 and 20 are operands and + is called the operator, which is performing the arithmetic operation and returning 30 as a result.
The operators are classified and listed according to precedence order.
Let's read and understand each operator one by one.
1. Arithmetic Operator
Arithmetic operators are used to performing mathematical operations such as addition (+), subtraction (-), multiplication (*), Division (/), and Modulus (%).
Arithmetic Operators are consist of many unary and binary operators that can be applied on one or two operands, the operands may be a values or variables.
Let's understand arithmetic operators with the help of the table below.
Here, a=20, b=10
Operator | Name | Description | Example | Output |
---|---|---|---|---|
+ | Addition | Addition of two values or variables | a + b | 30 |
- | Subtraction | Subtraction of two values or variables | a - b | 10 |
* | Multiplication | Multiplication of two values or variables | a * b | 200 |
/ | Division | Division of two values or variables | a / b | 2 |
% | Modulus | Remainder of division of two values or variables | a % b | 0 |
2. Assignment Operator
An assignment operator is used to assign a value to a variable. The assignment operator is denoted by the equals sign (=). It assigns a value to the right side operand or a value to the left side operand.
For example, int year = 2022; here, '=' is an assignment operator. It assigns the '2022' value to its left-side operand 'year'.
Java has provided the following types of assignment operators; see the table below.
Operator | Description | Example | Equivalent to |
---|---|---|---|
= | Assigns value from the right side operand to the left side operand. | z=y | z=y |
+= | It adds the right operand to the left operand and assigns the result to the left operand. | x +=y | x=x + y |
-= | It subtracts the right operand from the left operand and assigns the result to the left operand. | x -=y | x=x - y |
*= | It multiplies the right operand with the left operand and assigns the result to the left operand. | x *=y | x=x * y |
/= | It divides the left operand with the right operand and assigns the result to the left operand. | x /=y | x=x / y |
%= | It takes modulus using two operands and assigns the result to the left operand. | x %=y | x=x % y |
^= | Performs an exponential (power) calculation on operators and assigns value to the left operand. | x ^=y | x=x ^ y |
Bitwise operators work on binary digits (0 or 1) or bits of input values. They can be used with any integral type such as byte, short, int, and long.
Types of Bitwise Operators in Java
There are the following types of Bitwise Operators in Java they perform bit level operations.
See the give table of Bitwise Operators:
Operators | Symbol | Uses |
---|---|---|
Bitwise AND | & | op1 & op2 |
Bitwise OR | | | op1 | op2 |
Bitwise XOR | ^ | op1 ^ op2 |
Bitwise Compliment | ~ | ~op |
Bitwise left shift | << | op1 << op2 |
Bitwise right shift | >> | op1 >> op2 |
Unsigned Right Shift Operator | >>> op >>> | number of places to shift |
Logical operators are used to checking an expression or combination of expressions is true or false. This operator performs logical && (AND), || (OR) and ! (NOT) operations.
Just for understanding the below table let's assume X=10, Y=20
Operator | Operator Name | Description | Example | Output |
---|---|---|---|---|
&& | Logical AND | True if both the expressions are true otherwise false | ((X > 20) && (Y > 40)) |
false |
|| | Logical OR | True if either expression is true otherwise false | ((X > 20) || (Y < 40)) |
true |
! | Logical NOT | True if expression is false otherwise false | !((X > 20) && (Y > 40)) |
true |
Relational Operators are a bunch of binary operators that are used to check for relations between two operands. Relational Operators return either true or false as a result after the comparison. Relational Operations are extensively used in looping statements as well as conditional if-else statements and so on.
Just for understanding the below table we assume X=10, Y=20
Operators | Description | Example | Output |
---|---|---|---|
== | Equal operator | X == Y | false |
!= | Not Equal operator | X != Y | true |
> | Greater than operator | X > Y | false |
< | Less than operator | X < Y | true |
>= | Greater than or equal to operator | X >= Y | false |
<= | Less than or equal to operator | X <= Y | true |
The ternary operator is a conditional operator and it is a single line statement consisting of three parts as given below in the syntax.
The ternary operator is an alternative for using if-else and nested if-else statements.
In Java, Unary operators work with only one operand, It is used to perform operations like increment, decrement, negation, etc.
Let's breifly understand Unary Operators by given table
Operator Name | Operator Symbol | Description | Example | Equivalent Expression |
---|---|---|---|---|
Unary Plus | + | It is used to represent the positive value. | +a | a |
Unary Minus | - | It is used to represent the negative value. | -a | - |
Increment Operator | ++ | It increments the value of a variable by 1. | ++a or a++ | a=a+1 |
Decrement Operator | -- | It decrements the value of a variable by 1. | --a or a-- | a=a-1 |
Logical Complement Operator | ! | It inverts the value of a boolean variable. | !true | - |
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is a Modulus Operator?
Modulus operator is represented by % and returns the remainder of division operation.