Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.