BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Unary Operators in Java with Examples

By Chaitanya Singh | Filed Under: java

The word Unary means an operation that involves a single element. As the name suggests, The Unary operators in Java involve single operand. Java supports following unary operators:

  • Unary minus(-)
  • Increment(++)
  • Decrement(- -)
  • NOT(!)
  • Bitwise Complement(~)

1. Unary minus(-) Operator Example

The unary minus operator changes the sign of the operand. This operator is used on numbers, it changes the positive number to negative number and vice-versa.

int num = 100;
int num2 = -num; //value of num2 is -100

Difference between – arithmetic operator and – unary operator:

The – arithmetic operator involves two operands while the – unary operator involves a single operand.

int x = 10, y = 5, z = 100, sub; 

//this is minus(-) arithmetic operation
sub = x - y;

//this is minus (-) unary operation
int num = -z; //value of num is -100

Example of unary minus:

class JavaExample {
  public static void main(String[] args) {
    int number = 101;

    //opposite of number. The number with opposite sign is also called
    //additive inverse. The sum of number and it's additive inverse is zero
    int numberInverse = -number;

    System.out.println("The value of number is: "+number);
    System.out.println("Opposite of number is: "+numberInverse);
  }
}

Output:

output unary minus(-) operator

2. Increment(++) Operator Example

The increment(++) operator is used to increase the value of a variable. It can be used in following two ways:

  • Pre-increment (++i)
  • Post-increment (i++)
class JavaExample {
  public static void main(String[] args) {
    int number = 5;

    //increment unary operator
    number++;

    System.out.println(number);
  }
}

Output:

6

Difference between pre increment and post increment in java:

In pre-increment the value is incremented instantly, however in post-increment the value is incremented at the end of this statement and before executing next statement. This is shown in the following example:

Here the first if statement didn’t execute as the value of num1 is still 5 during comparison and only got increment after the condition is already evaluated. The second if statement executed because pre-increment increased the value instantly before reading the remaining statement, this made the value of num2 to 6 before comparison.

class JavaExample {
  public static void main(String[] args) {
    int num1 = 5, num2 = 5;

    //post-increment
    if(num1++ == 6){
      System.out.println("First if block");
      System.out.println("Value of num1: "+num1);
    }

    //pre-increment
    if(++num2 == 6){
      System.out.println("Second if block");
      System.out.println("Value of num2: "+num2);
    }
  }
}

Output:

Unary operator example output

3. Decrement(- -) Operator Example

The decrement(- -) operator is used to decrease the value of an operand. Similar to ++ operator, it can also be used in following two ways:

  • Pre-decrement (- -i)
  • Post-decrement (i- -)
class JavaExample {
  public static void main(String[] args) {
    int number = 5;

    //decrement unary operator
    number--;

    System.out.println(number);
  }
}

Output:

4

Difference between pre decrement and post decrement in java:

In pre-decrement the value is decremented instantly, however in post-decrement the value is decremented at the end of this statement and before executing next statement. For example:

class JavaExample {
  public static void main(String[] args) {
    int num1 = 5, num2 = 5;

    //post-decrement
    if(num1-- == 4){
      System.out.println("Post-decrement Operator");
      System.out.println("Value of num1: "+num1);
    }

    //pre-decrement
    if(--num2 == 4){
      System.out.println("Pre-decrement Operator");
      System.out.println("Value of num2: "+num2);
    }
  }
}

Output:

Post and pre-decrement operator java

4. NOT(!) Operator Example

The NOT(!) Operator reverses the logical state (true or false) of an operand. If an operand or condition is true, then the Logical NOT operator will make it false and vice-versa.

If value of a boolean variable 'b' is true, then !b is false
If the value of 'b' is false, then !b is true

Let’s take an example to understand this concept:

class JavaExample {
  public static void main(String[] args) {
    int totalMarks = 32;

    //if total marks are not greater than 33 then
    //the student is fail else pass
    if(!(totalMarks > 33)){
      System.out.println("Fail");
    }
    else{
      System.out.println("Pass");
    }
  }
}

Output:

Fail

5. Bitwise Complement(~) Operator Example

Bitwise complement is represented by ~ (Tilde) symbol. The bitwise complement is a 1’s complement representation of number. 1’s complement changes the 1’s to 0’s and 0’s to 1’s.
For example: In the following java program, we got -8 as a bitwise complement of 7, let’s see how we got this value.

Binary representation of number 7 = 111
1’s complement of this: 000 (reversed 1’s to 0’s and 0’s to 1’s)
decimal equivalent of this: 0

The compiler returns a 2’s complement of an input number, this means that compiler will print the 2’s complement of 0.

2’s complement= 1’s complement + add 1 to Least Significant Bit (LSB).
= 111 + 1
= 1000
Decimal equivalent of this 2’s complement is -8.

class JavaExample {
  public static void main(String[] args) {
    int num = 7;

    //print bitwise complement of 7
    System.out.println(~num);
  }
}

Output:

-8

Recommended Posts

  • Arithmetic Operators in Java with Examples
  • Assignment Operators in Java with Examples
  • Logical Operators in Java with Examples
  • Relational Operators in Java with Examples
  • Bitwise Operators in Java with Examples
  • Ternary Operator in Java with Examples
  • Shift Operators in Java with Examples
❮ Operators in Java

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap