Home Java Java-conversion The Unary Operators
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

The Unary Operators

Advertisement
In this example we will see how we can make use of the unary operator. In java we have been provided the unary operators so we should know how to make use of those operators.

The Unary Operators in Java

     

In this example we will see how we can make use of the unary operator. In java we have been provided the unary operators so we should know how to make use of those operators.  Unary operators are:

++expr, --expr, +expr, -expr, ~, !

Description of program:

In this section we will know how to make use of the unary  operators in java. First all of, we have to define a class "UnaryOperation" . Inside this class we have defined one class variable. We are making one overloaded main method which takes one integer type argument. Define one method operate() of type int where we are going to use the various unary operators.  At last inside the main method call the method operate() so that the result can be displayed to the user. The output will be displayed  on the screen by using the println() method of the System class. 

Here is the code of this program

class UnaryOperation {
  public static int i=0;
  public static void main(int i){
 i=i;
  }
  public static int operate(){
  int i = +1
  System.out.println(i);
  i--;  
  System.out.println(i);
  i++; 
  System.out.println(i);
  i = ++i; 
  System.out.println(i);
  boolean success = false;
  System.out.println(!success)
  System.out.println(success)
  return 0;
  }
  public static void main(String args[])
  {
  UnaryOperation uo= new UnaryOperation();  
  int i=uo.operate();
  }
  }

Download this example

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: June 25, 2008

Ask Questions?    Discuss: The Unary Operators  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Poxer@live.se
January 17, 2012
The ~ operator

I have got into contact with the ~ while reading someone else code and I can't find what it means. -(~((Integer) (int) obj.meth( ... I'm wondering what difference does the ~ make compared to if it was not there? Thankful for any answer, not necessarily about the code but about the operator in all. For security reasons I had to cut most of the code off.
DMCA.com