How do I use the unary operators?

The unary operators requires only one operand to operate on, it can perform operations such as incrementing or decrementing value by one, negating a value or inverting a value of a boolean expression.

The unary operators use the following symbols:

Symbol Description
+ unary plus operator; indicates positive value
- unary minus operator; negates a value
++ unary increment operator; increments value by one
-- unary decrement operator; decrements value by one
! unary logical complement operator; inverts a boolean value
package org.kodejava.example.fundamental;

public class UnaryOperatorsDemo {
    public static void main(String[] args) {
        int result = +10;  // result = 10
        System.out.println("result = " + result);
        result--;          // result = 9
        System.out.println("result = " + result);
        result++;          // result = 10
        System.out.println("result = " + result);
        result = -result;  // result = -10;
        System.out.println("result = " + result);

        // The increment and decrement operators can be applied
        // before (prefix) or after (postfix) the operand. Both
        // of them will increment or decrement value by one. The
        // different is that the prefix version evaluates to the
        // incremented or decremented value while the postfix
        // version evaluates to the original value;
        --result;
        System.out.println("result = " + result);
        ++result;
        System.out.println("result = " + result);

        boolean status = result == -10;  // status = true
        System.out.println("status = " + status);
        status = !status;                // status = false;
        System.out.println("status = " + status);
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply