Home Java Master-java Unary Operators
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Unary Operators

Advertisement
The unary operators requires only one operand to perform different kind of operations such as increasing/decreasing a value, negating an expression, or inverting a boolean value.

Unary Operators

     

The unary operators requires only one operand to perform different kind of operations such as increasing/decreasing a value, negating an expression, or inverting a boolean value. These operators can not be used with final variables. There are different types of unary operators mentioned in the table given below:

Symbol Name of the Operator Operation Example
 +    Unary plus operator  indicates positive value (however, numbers are positive without this)
int number = +1;
 -   Unary minus operator  negates an expression number = - number;
 ++    Increment operator  increments a value by 1 number = ++ number;
 --    Decrement operator  decrements a value by 1 number = -- number;
 !    Logical compliment operator  inverts a boolean value  

I. Unary Plus (+) 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:

int x = 0;
int y = (-25);
x = (+y);

In this expression, a negative value is assigned to the variable "y". After applying  unary plus (+) operator on the operand "y", the value becomes 25 which indicates it as a positive value.
However a number is positive without using
unary plus (+) operator, if we have initially assigned it positively into the operand in the program.

II. Unary minus (-) Operator

Unary minus operator (-) indicates negative value and differ from the unary plus operator. This (-) operator is also used to perform a type conversion operation on an operand. If a value of the integer operand is positive then that value can be produced as a negatively applying unary minus (-) operator.  For example, lets see the expressions shown as:

int x = 0;
int y = 25;
x = (-y);

In this expression, a positive value is assigned tothe variable "y". After applying  minus plus (-) operator on the operand "y", the value becomes "-25"  which indicates it as a negative value. This behavior represents the number in two's complement format.

III. Increment (++) and Decrement Operators

The increment/decrement operators can be a prefix or a postfix .In a prefix expression (++ x or -- x), an operator is applied before an operand while in a postfix expression (x ++ or x --) an operator is applied after an operand. In both conditions 1 is added to the value of the variable and the result is stored back to the variable. However both operators have the same effect as "x = x + 1;"

Although there is a major difference between a prefix and a postfix expressions. In a prefix expression, a value is incremented first then this new value is restored back to the variable. On the other hand, In postfix expression the current value is assigned to a variable then it is incremented by 1 and restored back to the original variable.

Lets make the things more clear with few examples
( i ) example using prefix unary operator:

public class  Prefix{
  public static void main(String[] args){
 int x = 0;
 int y = 0;
 y = ++x;
 System.out.println("The value of x :" + x);
  System.out.println("The value of y:" + y);
  }
}

Output of the Program:

C:\nisha>javac Prefix.java

C:\nisha>java Prefix
The value of x :1
The value of y:1

The output of this program shows that always 1 is stored in both variables i.e. the value of "x" is incremented first then it is assigned to the variable "y".

( ii ) example using postfix unary operator:

public class Postfix{
  public static void main(String[] args){
 int x = 0;
 int y = 0;
 y = x++;
 System.out.println("The value of x :"+ x );
 System.out.println("The value of y:" + y );
  }
}

Output of the Program:

C:\nisha>java Postfix
The value of x :1
The value of y:0

The output of the program indicates  the value of variable "x" is stored first to the variable "y" then it is incremented by 1.

 IV. Logical Compliment (!) Operator

The logical compliment (!) operator is also known as Boolean Negation Operator. It is used to invert the value of a boolean type operand i.e. the type of the operand  must be boolean while using this operator,. If the value of the boolean operand is false, the ! operator returns true. But, if the value of the operand is true, the ! operator returns false

For example, lets see the statements shown as:

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

In these statements, the first expression returns true to the variable "result" but at the end of the program, the output is shown as false because the compliment (!) operator inverts the value of the variable "result"

Lets have one more example using unary operators in different flavors:

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 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 of the Program:

C:\nisha>javac UnaryOperator.java

C:\nisha>java UnaryOperator
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

Download this Program

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Ask Questions?    Discuss: Unary Operators   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
amir khan
June 13, 2013
i dont like ur answer

bcz ur answer is so big i need sort answer plzzzzzz reply
Wendy Xu
September 19, 2011
Java Unary Operators

This was really helpful! It helped me get a better understanding of Java unary operators! The unary operator chart was clear and explicit, and so were the examples! Good job for the one who constructed this webpage!
uvi
December 28, 2011
unary plus

For C programming,in the unary plus example, if you assign a negative value to y and then assign +y to x, the result is negative, so my program goes like given below to get the positive value 25. #include<stdio.h> #include<conio.h> void main() { int x,y; clrscr(); y=(-25); x=(-y); printf("%d",x); getch(); }
Rakesh kumar sharma
December 7, 2012
java unary operator { i need help to understand}

int i=0; k=i++; int j =0; j= j++; sop(i); sop(k); sop(j); ----------------------------- my Question is that latest value of i is assign to sop)(i)...then the output is 1 similarly in K there is post increment so first value is assign to K then there is increment will took place . In the same way when there is post increment take place in j ..then why not latest value of j should be printed out i.e 1 instead of 0 the output of the program is : 1 0 0
Rakesh kumar sharma
December 7, 2012
i need help to understand this .plz help me

int i=0; k=i++; int j =0; j= j++; sop(i); sop(k); sop(j); ----------------------------- my Question is that latest value of i is assign to sop)(i)...then the output is 1 similarly in K there is post increment so first value is assign to K then there is increment will took place . In the same way when there is post increment take place in j ..then why not latest value of j should be printed out i.e 1 instead of 0 the output of the program is : 1 0 0
DMCA.com