dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Understanding the Unary Operator in Java

See a few examples of how to use the unary operator.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Java uses the symbol tilde (~) to represent the operator. The following examples are clearly illustrative of the use and outcome of this operator.

Code sample:

public class JavaUnary
{
public static void main(String args[])
{
JavaUnary javaUnary = new JavaUnary();
javaUnary.proceed();
}

private void proceed()
{
System.out.println("Unary (~) of 55: " + ~ 55 ); //Output is 1 number lesser than the negative of the argument
System.out.println("Unary (~) of -55: " + ~ -55 ); //Output is 1 number lesser than the positive of the argument
}
}
/*

Expected output:

[root@mypc]# java JavaUnary
Unary (~) of 55: -56
Unary (~) of -55: 54
*/

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Thanks for your registration, follow us on our social networks to keep up-to-date