What are Operators in Java?

What are Operators in Java

In this tutorial, we will clarify what are operators in Java. We will also focus on why operators are used in Java and also see how they are used in a Java program.

What are Operators in Java?

Operators in Java are special symbols that have a predefined meaning in the Java compiler that perform some operation with one or more operands and then return an output (value).

What are Operators in Java

8 Types of operators in Java

  1. Arithmetic Operators in Java
  2. Relational Operators in Java
  3. Logical Operators in Java
  4. Assignment Operators in Java
  5. Bitwise Operators in Java
  6. Shift Operators in Java
  7. Unary Operators in Java
  8. Ternary Operators in Java

What are Arithmetic Operators in Java?

Arithmetic Operators are used in arithmetic or mathematical type of operations. These operations can be multiplication, division, addition, etc.

OperatorOperationReturnsExample
+Performs addition of values on both side of the operatorSum of values2 + 5 returns 7
Performs subtraction of values by subtracting the value on the right-hand side from the value on the left-hand side of the operatorDifference of values5 – 4 returns 1
*Performs multiplication of values on both side of the operatorProduct of values3 * 4 returns 12
/Performs division of value on the left-hand side with the value on the right-hand side of the operatorThe quotient obtained by division10 / 2 returns 5
%Performs division of value on the left-hand side with the value on the right-hand side of the operatorRemainder obtained by division11 / 2 returns 1

What are Relational Operators in Java?

Relational Operators are used as a condition to check whether the output is true or false.

OperatorOperationReturnsExample
==Verifies if the operand on the left-hand side of the operator is equal to the right-hand side of the operatorTrue if both the values are equal, if not then false36 == 36 returns True
<Verifies if the operand on the left-hand side is smaller than the operand on the right-hand side of the operatorReturns True if the left operand is smaller than the right operand, else return False53 < 24 returns False
>Checks if the operand on the left-hand side is greater than the operand on the right-hand side of the operatorReturns True if the left operand is bigger than the right operand, if not then returns False53 > 24 returns True
<=Checks if the operand on the left-hand side is smaller than or equal to the operand on the right-hand side of the operatorReturns True if the left operand is smaller than or equal to the right operand, else returns False23 <= 23 returns True
>=Checks if the operand on the left-hand side is greater than or equal to the operand on the right-hand side of the operatorReturns True if the left operand is greater than or equal to the right operand, else returns False23 >= 24 returns False
!=Verifies if the operand on the left-hand side of the operator is not equal to the right-hand side of the operatorReturns True if the left operand is not equal to the right operand, else returns False5 != 64 returns True

What are Logical Operators in Java?

A Logical operator is a special symbol that takes one or two conditions (based on the operator) and returns the logical output in the form of true/false.

OperatorOperationReturnsExample
&&Performs logical AND operation on either side of the operatorTrue if both the conditions are true, if not then False2<4 && 3>1 returns true
||Performs logical OR operation on either side of the operatorTrue if one or both the conditions are true, if not then False2<4 || 3<1 returns true
!Performs logical NOT operation on a single operandReturn True if the condition is False. Returns False if the condition is True!(23>4) return False

What are Assignment Operators in Java?

An Assignment Operator is basically used to simply assign a value to a variable.

OperatorOperationExample
=Assigns the value on the right-hand side to the operand on the left-hand side of the operatorv = 23
+=Assigns the sum of operands on either side to left-hand side operandv += 43
-=Assigns the difference of operands on either side to left-hand side operandv -= 73
*=Assigns the product of operands on either side to the left-hand side operandv *= 32
/=Assigns the quotient obtained by division of operands on either side of the operator to left-hand side operandv /= 87
%=Assigns the remainder obtained by division of operands on either side of the operator to left-hand side operandv %= 12
^=Performs left operand to the power of right operand and assigns its result to left operandv ^= 3

What are the Bitwise Operators in Java?

Bitwise Operators are used to performing per bit of operations. If there are two values say 4 and 6, then the bitwise operation will be performed on their binary form.

OperatorOperationReturnsExample
&Performs bitwise AND operation on both the operandsBitwise AND of operands4&4 returns 4
|Performs bitwise OR operation on both the operandsBitwise OR of operands12|9 returns 13
^Performs bitwise XOR operation on both the operandsBitwise XOR of operands12|9 returns 5

What are Shift Operators in Java?

Shift Operators are used for shifting purposes. Shift Operators are used to shifting bits if a number either left to right or right to left. Shifting also causes a number to either multiply or divide based on the type of shifting.

There are two types of shifting that can be done in Java:

  1. Left Shift (<<)
  2. Right Shift (>>)

Shift Operators are represented by pairs of (less than/greater than) symbols.

What are the Unary Operators in Java?

Unary Operators are used to performing unary operations on a single operand. They are used to either increment a value or decrement a value by 1.

OperatorOperationReturnsExample
++Used to increment a valueReturns Previous value plus 1 when used before a variable/value otherwise returns the previous value and then increments the variable/value by 1a++, ++a
– –Used to decrement a valueReturns Previous value minus 1 when used before a variable/value otherwise returns a previous value and then decrements the variable/value by 1b- -, – -b
!Used to invert a Boolean valueReturns True if the value is False, else Return False!c

What are the Ternary Operators in Java?

A Ternary operator has one condition and two outputs. When the condition is True, the first output is returned, if not then the second output is returned.

The basic form of a Ternary operator is:
(condition) ? (Output 1) : (Output 2)

Example:

class Program {
    public static void main (String agmt[]) {
        
        //Using Assignment Operator
        int n1 = 25;
        int n2 = 58;
        int n3 = 76;
 
        //Using Relational Operator
        if(n2 > n1)
            System.out.println("n2 is greater than n1");
        
        //Using Arithmetic Operator
        n3 = n1 + n2;
 
        //Using Logical Operator
        if (n3>n1  && n3>n2)
            System.out.println("n3 is the greatest number");
    }
}

Output

n2 is greater than n1

n3 is the greatest number

In the above example, we can see the variables n1, n2, and n3 are being assigned values using an Assignment Operator (=). The if-condition is using a Relational Operator (>) to compare the values of n2 and n1. Finally, we have used a Logical Operator (&&) to check if the conditions on either side are true are not.

Previous articleConstants in Java Programming
Next articleWhat are Methods in Java?
I’m Alimam Miya, a passionate educator and blogger dedicated to providing accurate and reliable educational content. With experience in programming, technical education, and competitive exam preparation, I simplify complex topics into easy-to-learn resources. Through my blogs, I share free study materials, courses, and updates on exams like GATE and IIT-JEE to help students in their learning journey