JAVA programs

Java Program Using Unary Operator

In this program we see the use of unary operators. Following program explain how unary operator works.

Program

class UnaryOperator

{

    public static void main(String[] args)

    {

        int number=1;

        int number1=0;

        System.out.println(" result is now:"+number);      

        number=-number;

        System.out.println(" result is now:"+number);          

        ++number;

        System.out.println(" result is now:"+number);

       

        number1=number++;

        System.out.println(" result is now:"+number);

        System.out.println(" result is number1:"+number1);

               

        boolean result=(2>1);

        System.out.println(" 2 is greater than 1: "+result);       

        System.out.println(" 2 is greater than 1: "+!result);

    }}

When Run The Program Following Output:

 result is now:1

 result is now:-1

 result is now:0

 result is now:1

 result is number1:0         

 2 is greater than 1: true

 2 is greater than 1: false

Description

Unary operators are those operators that work on only one operand.

Following are some unary operators.

  (Unary minus) this unary operator is used for  negating the values.

+   (Unary plus)  this unary operator is used for giving positive values.

++ (Increment operator) this unary operator is used for incrementing the value by 1.

— (Decrement operator), this unary operator is used for decrementing the value by 1

!   (Logical not operator), this unary operator is used for inverting a Boolean value.

Releted Programs

© 2017 codewmAll rights reserved.