Friday 21 October 2016

Unary operators

Unary operators

  • Unary Operators are,
      • + unary plus
      • -  unary minus
      • ++ increment
      • --  decrement
      • Logical complement operator
These are all operators which act upon a single operand
Operator
Meaning
+
Unary plus operator; 
indicates positive value 
(all numbers are 
positive by default, 
even without this operator).
-
Unary minus operator; 
to get negative values.
++
Increment operator; 
increments a value by 1.
--
Decrement operator; 
decrements a value by 1;
!
Logical complement operator; 
inverts value of a boolean.

 ---------------------------------------------------------------------------------------
Program         :       Program on Unary operators
Program name  :       UOpeDemo1.java
Output                  
                              +x = 10
      -y = -20
      ++x = 11
      ++y = 19
      false

class UOpeDemo1
{
        public static void main(String args[])
        {
               int x = 10;
               int y = 20;
                      int unaryPlus= +x;
               int unaryMinus= -y;
               int increment = ++x;
               int decrement= --y;
               boolean status= false;
               
               System.out.println("+x = " + unaryPlus);
               System.out.println("-y = " + unaryMinus);
               System.out.println("++x = " + increment);
               System.out.println("++y = " + decrement);
               System.out.println(status);
               System.out.println(!status);
        }
}

Compile     :            javac UOpeDemo1.java
Run           :            java UOpeDemo1
Output             
                              +x = 10
      -y = -20
      ++x = 11
      ++y = 19
      false
---------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan