View RSS Feed

My Java Tips

Unary operators

Rating: 1 votes, 1.00 average.
by , 11-02-2011 at 07:09 PM (1399 Views)
I will present an an example that will show the use of following uniary operators: Unary minus operator, Increment operator, Decrement operator, Logical compliment operator


Unary plus operator (+) indicates positive value. This (+) operator is used to perform a type conversion operation on an operand. The type of the operand must be an arithmetic data type i.e. if a value of the integer operand is negative then that value can be produced as a positively applying unary plus (+) operator. For example, lets see the expressions shown as:

Java Code:
     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 of number1 is:" + number1);

      boolean result = (2>1);
      System.out.println("2 is geater than 1: " + result);
      System.out.println("2 is geater than 1: " + !result);
     }
}
Output:


result is now:1
result is now:-1
result is now:0
result is now:1
result of number1 is:0
2 is geater than 1: true
2 is geater than 1: false

Submit "Unary operators" to Facebook Submit "Unary operators" to Digg Submit "Unary operators" to del.icio.us Submit "Unary operators" to StumbleUpon Submit "Unary operators" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments