Course Content
Core Java
About Lesson

Unary Operator

Unary operators require only one operand. They are used to perform various operations:

  1. incrementing/decrementing a value by one
  2. negating an expression
  3. inverting value of the boolean

Arithmetic Operator

They are used in mathematical expressions. Operands of arithmetic operators must be of numeric type and char (char in Java is a subset of int).

  1. + Unary Plus: it returns value of its single operand.
  2. - Unary Minus: It negates its single operand.
  3. + - * / : It would work like it works in general maths.
  4. % Modulus : it returns remainder of division operation. Can be applied to floating types and integer types.
  5. += -= *= %= /= : they are compound assignment operator. eg i = i + 2 is same as i+= 2 i = i / 2 is same as i /= 2
  6. ++ Increment -- Decrement : It increases/decreases operand by one respectively.
    1. Postfix: i++ i-- When operators used in large expression (eg int i = 2; i = i++ + ++i ), previous value is obtained for used in expression and then operand is modified.
    2. Prefix: ++i --i When operators used in large expression (eg int j = 10; j = --j + ++j ), operand is incremented or decremented before the value is obtained for use in the expression.
  7. -- Decrement : It decreases operand by one.
public class ArithmeticOperatorsExample {

    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        // Addition (+): Adds two operands
        int sum = a + b; // 10 + 5 = 15
        System.out.println("Addition: " + sum);

        // Subtraction (-): Subtracts the second operand from the first
        int difference = a - b; // 10 - 5 = 5
        System.out.println("Subtraction: " + difference);

        // Multiplication (*): Multiplies two operands
        int product = a * b; // 10 * 5 = 50
        System.out.println("Multiplication: " + product);

        // Division (/): Divides the first operand by the second (integer division)
        int quotient = a / b; // 10 / 5 = 2
        System.out.println("Division: " + quotient);

        // Modulus (%): Returns the remainder of the division of the first operand by the second
        int remainder = a % b; // 10 % 5 = 0 (no remainder)
        System.out.println("Modulus: " + remainder);

        // Increment (++): Increases the value of the operand by 1
        a++; // Equivalent to a = a + 1; (a becomes 11)
        System.out.println("Increment: " + a);

        // Decrement (--): Decreases the value of the operand by 1
        b--; // Equivalent to b = b - 1; (b becomes 4)
        System.out.println("Decrement: " + b);

        // Compound Assignment Operators: Combine arithmetic operations with assignment
        a += 2; // Equivalent to a = a + 2; (a becomes 13)
        System.out.println("Compound Assignment (Addition): " + a);

        b *= 3; // Equivalent to b = b * 3; (b becomes 12)
        System.out.println("Compound Assignment (Multiplication): " + b);
        
        // Postfix Increment: Increases the value of the operand by 1, returns the original value
        int postIncrementA = a++; // a = 10, postIncrementA = 10 (a becomes 11)
        System.out.println("Postfix Increment: " + postIncrementA + ", a: " + a);

        // Prefix Increment: Increases the value of the operand by 1, returns the updated value
        int preIncrementB = ++b; // b = 6, preIncrementB = 6
        System.out.println("Prefix Increment: " + preIncrementB + ", b: " + b);

        // Postfix Decrement: Decreases the value of the operand by 1, returns the original value
        int postDecrementA = a--; // a = 11, postDecrementA = 11 (a becomes 10)
        System.out.println("Postfix Decrement: " + postDecrementA + ", a: " + a);

        // Prefix Decrement: Decreases the value of the operand by 1, returns the updated value
        int preDecrementB = --b; // b = 5, preDecrementB = 5
        System.out.println("Prefix Decrement: " + preDecrementB + ", b: " + b);
    }
}
Scroll to Top