devxlogo

Understanding the Unary Operator in Java

Understanding the Unary Operator in Java

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 argumentSystem.out.println("Unary (~) of -55: " + ~ -55 ); //Output is 1 number lesser than the positive of the argument}}/*

Expected output:

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

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist