📑

On this page — Quick navigation

👇 Click any topic to scroll directly

Java operators are the symbols that are used to perform various operations on variables and values. By using these operators, we can perform operations like addition, subtraction, checking less than or greater than, etc.

There are different types of operators in Java, we have listed them below −

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Misc Operators

Java Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −

Assume integer variable A holds 10 and variable B holds 20, then −

Operator Description Example
+ (Addition) Adds values on either side of the operator. A + B will give 30
– (Subtraction) Subtracts right-hand operand from left-hand operand. A – B will give -10
* (Multiplication) Multiplies values on either side of the operator. A * B will give 200
/ (Division) Divides left-hand operand by right-hand operand. B / A will give 2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0
++ (Increment) Increases the value of operand by 1. B++ gives 21
— (Decrement) Decreases the value of operand by 1. B– gives 19

Example

The following example demonstrates the usage of arithmetic operators in Java:

2. Subtraction Operator (-)

The subtraction operator subtracts one number from another.

java
public class SubtractionExample {
    public static void main(String[] args) {
        int x = 50;
        int y = 30;
        int difference = x - y;
        
        System.out.println(x + " - " + y + " = " + difference);
        
        // Negative result
        int result = 20 - 50;
        System.out.println("20 - 50 = " + result);
        
        // Multiple subtractions
        int finalValue = 100 - 30 - 20 - 10;
        System.out.println("100 - 30 - 20 - 10 = " + finalValue);
    }
}
/* Output:
50 - 30 = 20
20 - 50 = -30
100 - 30 - 20 - 10 = 40
*/

3. Multiplication Operator (*)

The multiplication operator multiplies two or more numbers.

java
public class MultiplicationExample {
    public static void main(String[] args) {
        int length = 10;
        int width = 5;
        int area = length * width;
        
        System.out.println("Area of rectangle: " + area);
        
        // Multiplying multiple numbers
        int product = 2 * 3 * 4 * 5;
        System.out.println("2 * 3 * 4 * 5 = " + product);
        
        // Multiplication with different data types
        double price = 25.5;
        int quantity = 3;
        double total = price * quantity;
        System.out.println("Total cost: $" + total);
    }
}
/* Output:
Area of rectangle: 50
2 * 3 * 4 * 5 = 120
Total cost: $76.5
*/

4. Division Operator (/)

The division operator divides one number by another.

Integer Division vs Floating-point Division

java
public class DivisionExample {
    public static void main(String[] args) {
        // Integer division (truncates decimal part)
        int a = 15;
        int b = 4;
        int intResult = a / b;
        System.out.println("15 / 4 = " + intResult);  // Output: 3
        
        // Floating-point division (preserves decimal)
        double x = 15.0;
        double y = 4.0;
        double doubleResult = x / y;
        System.out.println("15.0 / 4.0 = " + doubleResult);  // Output: 3.75
        
        // Mixed division
        double mixedResult = 15 / 4.0;
        System.out.println("15 / 4.0 = " + mixedResult);  // Output: 3.75
    }
}

5. Modulus Operator (%)

The modulus operator returns the remainder of a division operation.

java
public class ModulusExample {
    public static void main(String[] args) {
        // Even/Odd check
        for (int i = 1; i <= 5; i++) {
            if (i % 2 == 0) {
                System.out.println(i + " is even");
            } else {
                System.out.println(i + " is odd");
            }
        }
        
        // Remainder examples
        System.out.println("17 % 5 = " + (17 % 5));  // 2
        System.out.println("20 % 6 = " + (20 % 6));  // 2
        System.out.println("15 % 3 = " + (15 % 3));  // 0
        
        // Negative numbers
        System.out.println("-10 % 3 = " + (-10 % 3)); // -1
        System.out.println("10 % -3 = " + (10 % -3)); // 1
    }
}
/* Output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
17 % 5 = 2
20 % 6 = 2
15 % 3 = 0
-10 % 3 = -1
10 % -3 = 1
*/

 

Java Assignment Operators

Assignment Operators are used to assign values to variables. These operators modify the value of a variable based on the operation performed. The most commonly used assignment operator is =, but Java provides multiple compound assignment operators for shorthand operations.

Following are the assignment operators supported by Java language −

Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand. C = A &plus; B will assign value of A &plus; B into C
&plus;= Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C &plus;= A is equivalent to C = C &plus; A
-= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C − A
&ast;= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. C &ast;= A is equivalent to C = C &ast; A
/= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

Example

The following example demonstrates the usage of assignment operators in Java: