# Operators in Java

Video Tutorial
FREE
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Operators can be easily defined as characters that represent an operation. These symbols perform different operations on several variables and values. There are a total of 8 types of operators in Java: Unary, Arithmetic, Shift, Relational, Bitwise, Logical, Ternary, and Assignment operators. In this article, we will learn about operators in Java and their types.

## What are Java Operators?

Operators are the formulas that can perform an operation involving the values of the operands. Let's talk about an example 5 + 6 = 11. Here, 5 and 6 are the operands, and + is called the operator. We can also use operators for a single operand as well as more than two operands in some situations. Here we are going to be talking about Java operators.

## Types of Java Operators

The list given below informs us about the entire range of operators that are provided to us by the Java programming language.

1. Arithmetic Operator
2. Unary Operator
3. Assignment Operator
4. Relational Operator
5. Logical Operator
6. Ternary Operator
7. Bitwise Operator
8. Shift Operators
9. instanceof Operator

Let’s look more deeply into each one!

### Arithmetic Operator

When you want to perform normal basic mathematical functions like addition, subtraction, multiplication, division, etc., Arithmetic Operators are used. They can be performed on primitive data types.

OperatorNameFunctionSyntax
-SubtractionSubtraction of two valuesx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides two values. If we divide a number by 0, it will either raise ArithmeticException, or it will result in NaN or (+-)Infinity values.x / y
%ModulusThe modulus operator in Java returns the remainder of the two numbers after division.x % y

When we use an integer and a floating point number are used as operands to a single arithmetic operation, the answer is a floating point number. We have summarized the data type returned by the arithmetic operators based on the implicit type conversion for operands.

Data Type of ResultData Type of Operands
longNeither operand is a float or double. At least one of the operands is long.
intNeither operand is a float or a double or a long.
doubleAt least one operand is a double
floatAt least one operand is float: neither operand is a double

Example:

Output

### Unary Operators

Unary Operators, as the name suggests, need only a single operand, and as they are operators, they are used to increment a value, decrement, or eliminate it.

Different kinds of unary operators are:

OperatorNameDescription
++Increment OperatorIncrement operator increments the value of the operand by 1. This operator can be used as a post-increment and as a pre-increment. Post increment is used when we want to use the current value and then perform the incrementation. On the other hand, pre-increment is used when we perform the incrementation first and then use the value.
--Decrement OperatorDecrement operator decreases the value of the operand by 1. (opposite of increment operator) As we've seen in the above increment operator, this operator also has two use cases, the first being post-decrement and the second as pre-increment having similar behavior as explained above.
+Unary Plus OperatorPresents the positive value. It can perform an automated conversion to int when the other types are byte, char, and short.
-Unary Minus OperatorNormally used for eliminating or negating values.
!Logical not OperatorIt is used for inversion of the boolean value. This operator reverses the value of a boolean expression. Let's say a boolean value is true and if we use the! operator with a true expression, it will give us the result as false. So it inverses the boolean value and returns the result.

Unary Operators in Java are used to either increment or decrement the value by one. Let's see an example.

Example

Output

### Assignment Operator

As the name suggests assignment operator assigns a value to any variable. It uses the right to left associativity. This means that the value given on the RHS of the assignment operator is given to the variable on the LHS, which leads to the RHS being calculated before being able to use it.

The assignment operators can be used as Compound Assignment Operators, which provide a shorter syntax for assessing the result of an arithmetic operator. Let's see some examples.

OperatorExampleEquivalent to
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y

Assignment Operators in Java are used for modifying a value to perform some mathematical calculations and for assigning values to the operands.

Example:

Output

### Relational Operator

Operators that are used for checking the relations like greater than, less than, equal to, or not equal between the two operands are known as Relational Operators. It returns the result as a boolean value which later on, after comparison and is substantially used in the process of looping and conditional if-else statements.

SymbolNameFunction
<Less thanThe value returned will be true if the RHS value is greater than the LHS value.
>Greater thanThe value that is returned will be true if the LHS value is greater than the RHS value.
==Equal ToThe value returned will be true if the LHS value is equal to the RHS value.
!=Not Equal ToThe value returned will be true if the LHS value is not equal to the RHS value
<=Less than or Equal ToThe value returned will be true if the LHS value is less than or equal to the RHS value.
>=Greater than or Equal ToThe value returned will be true if the LHS value is greater than or equal to the RHS value.

These operators can be used to compare two operands, for example, we can use relational operators in Java to compare the age of two persons, the price of two items, etc.

Example

Output

### Logical Operator

Operators that are used to check if an expression given is true or false are called Logical Operators.

Usually, they are used to perform certain functions like 'logical AND' and 'logical OR'. Unlike relational operators which just compare a value and return TRUE or FALSE, logical operators evaluate the logic in the form of TRUE or FALSE. Values used in logical operations are first converted into boolean values and then evaluated.

Logical Operators in Java

NameFunction
Logical ANDValue returned is true when both of the given conditions are true.
Logical ORValue returned is true when at least one given condition is true (If the first operand is true, the second operand is not evaluated).
Logical NOTIt has the power to reverse the overall result. For example, if the value returned is true, it gives out false.

Example:

Output

### Ternary Operator

Ternary operators in Java are used to assign the value of an expression to a variable post-evaluation of a condition. Ternary operator allows us to translate multiple lines of code of an ‘if-else’ block into a single line. It is an alternative for applying If-else and the nested else-if statements. The execution order for ternary operators is from the left side to the right side.

Syntax

• Condition: It represents the condition that is written in an if statement.
• Expression1: This expression will be stored in the Variable if the condition is true.
• Expression2: This expression will be stored in the Variable if the condition is false.
• Variable: It stores the value returned by either expression.

Example

Output

### Bitwise Operators

The operators that work directly on bits are called Bitwise Operators. Normally, on our computers, the representation of numbers is done by bits: series of zeros and ones. When you need to perform operations on individual bits, they are used. It can be used with any integer type.

Types of Bitwise Operators in Java-

OperatorFunctionExample
Bitwise ANDThe Bitwise AND operator returns one if both the operands are 1. We can also say that it returns the bit-by-bit AND of the inputs provided.x & y
Bitwise ORReturns the bit-by-bit OR of the inputs provided. This operator returns one if at least one of the bits is 1.x | y
Bitwise XORThis operator returns one if exactly one of the operands is 1, but if both the operands are either 0 or 1, it returns 0. It performs bit-by-bit xor for multi-bit inputs and returns the result.x ^ y
Bitwise ComplementThis operator returns the complement of the operand; it is a unary operator. It returns one if 0 is the operator and vice versa. We can also say that it returns one’s complement while all the bits will be reversed.~x

In the given program, we have evaluated and described all the Bitwise operators in Java using a simple yet elaborative code-

Output

### Shift operators

NameFunction
Left Shift Operator (<<)The Left Shift operator shifts the bits of the left operand to the left by the number of positions indicated by the right operand, essentially multiplying the operand by two with each shift.
Signed Right Shift Operator (>>)The right shift operator shifts the bits from the left operand to the right by the number of places provided by the right operand. The sign bit is extended to preserve the sign of the original number, effectively dividing the operand by 2 for each shift.
Unsigned Right Shift Operator (>>>)Shifts the bits of the left operand to the right by the number of places specified by the right operand, filling the leftmost bits with zeros regardless of the sign, which may change the operand sign.

Example:

Output:

### instanceof Operator

The Instanceof Operators in Java will crosscheck if an object is of a certain class or not. The instanceof operator works on is-a relationship. It is also used for checking the data type of a variable.

Syntax:

Example:

Output

## Precedence and Associativity of Operators in Java

Precedence of Operators in Java is used when dealing with equations involving more than one type of operator. Using these rules, we find out which part of the equation needs to be evaluated first, as there are many various values for a particular equation.

NameOperatorsAssociativity
Postfix++, --Left to Right
Unary+, -, !, ++, ~, --Right to Left
Multiplicative*, /, %Left to Right
Shift<<, >>Left to Right
Relational<, <=, >, >=Left to Right
Equality==, !=Left to Right
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
Bitwise OR|Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Conditional?Right to Left
Assignment=, +=, -=, /=, %=, >>=, <<=, &=, ^=Right to Left

1. Simplicity and Readability: Java operators simplify the syntax for performing arithmetic, logical, and other operations, making the code more readable and understandable.

2. Efficiency: Operators are designed to be efficient and perform operations quickly.

3. Rich Set of Operations: Java provides a wide range of operators (arithmetic, bitwise, logical, etc.), allowing developers to perform various operations easily without requiring lengthy method calls or complex logic.

1. Limited Operator Overloading: Unlike languages such as C++, Java does not allow developers to overload operators for their own classes, which can limit the expressiveness and readability of custom data types.

2. Limitation in Customizability: Operators' fixed functionality means that their behavior cannot be adapted for custom types beyond what the language inherently provides, unlike methods, where behavior is defined by the developer.

## Conclusion

• Operators in Java are the formulas that can manipulate the values of the operands.
• Operators in Java can use two or even one operand at a time, according to their functionality. -There are 8 types of operators in Java: unary, Relational, Assignment, java instanceOf, Ternary, Arithmetic, Logical, and Bitwise Operators.
• Precedence of Operators in Java is used when dealing with equations involving more than one type of operator in them.

## FAQs

Q. What are operators in Java?

A. Operators in Java are special symbols that perform operations on one, two, or three operands and return a result. For example, the addition operator (+) adds two numbers, and the equality operator (==) compares two values for equality.

Q. What is the >>> operator in Java?

A. The >>> operator is the unsigned right shift operator. It shifts the zero-filled bits of its left-hand operand to the right by the number of bits specified by its right-hand operand.

Q. What is == in Java?

A. The == operator in Java is used to compare two values for equality.