Java: Unary Operator

In java programming language there is a type of operator called unary operator which does the job of operations like incrementing, decrementing, or negating operand. It consists of the various arithmetic and logic into one single operator which operates on this single basis.

Let's have a look at some of the unary operators of the java programming language.

1. Unary Minus (-)

The operator is used to brand an operand a negative value from positive.

Syntax

-(Operand)

Example

public class Test 
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10,val5;

       val3 = val1 + val2;
       val5 = -val3;
       System.out.println(val5);
    }
}

Output: -30

2. Unary Not Operator (!) 

With the sign of negation this operator reverses the logical state of the provided operator by giving out the true and false scenario.

Syntax

!(Operand)

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10, val2 = 20, val3, val4 = 10, val5;

        val3 = val1 + val2;
        val5 = val3;
       System.out.println(!(val5 == val2));
    }
}

Output: True

3. Increment (++)

Mostly used to increase the value of the integer with the operand. Consists of two types.

3.1 Post-Increment Operator

So the value of the integer increases when this is placed after the variable previous value gets retained until this is executed at its statement.

Syntax

var++

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10, val2 = 20, val3, val4 = 10 ,val5;

        val3 = val1 + val2;
        val5 = val3;
       
        System.out.println(val5++);
    }
}

Output: 30

3.2 Pre-Increment Operator

When this operator is placed before the operator is incremented in an instant.

Syntax

++var

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10, val5;

        val3 = val1 + val2;
        val5 = val3;
        
        System.out.println(++val5);
    }
}

Output: 31

4. Decrement (--)

Mostly used to decrease the value of the integer with the operand. Consists of two types.

4.1 Post-decrement Operator

So the value of the integer decreases when this is places after the variable previous value gets retained until this is executed at its statement.

Syntax

var--

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10, val5;

        val3 = val1 + val2;
        val5 = val3;
   
        System.out.println(val5--);
    }
}

Output: 30

4.2 Pre-Decrement Operator

When this operator is placed before the operator is decremented in an instant.

Syntax

--var

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10, val5;

        val3 = val1 + val2;
        val5 = val3;
   
        System.out.println(--val5);
    }
}

Output: 29

Thu, 10/14/2021 - 14:59

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags