Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Q.A Academy
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java
  • What is Unary Operators in Java? 

What is Unary Operators in Java? 

What is Unary Operators in Java? 

Unary operators in Java are operators that operate on a single operand to perform various operations, such as incrementing/decrementing a value, negating a boolean value, or inverting the sign of a number. Java provides several unary operators:

  1. Unary Plus (+)
  2. Unary Minus (–)
  3. Increment (++)
  4. Decrement (—)
  5. Logical NOT (!)

1. Unary Plus (+) #

The unary plus operator is used to indicate a positive value. Though it doesn’t change the value, it’s sometimes used for code readability.

Example:

public class UnaryOperatorExample {

    public static void main(String[] args) {

        int a = +5; // Positive value

        System.out.println("a = " + a); 

    }

}

Output: 
a = 5

2. Unary Minus (–) #

The unary minus operator negates the value of the operand, effectively changing its sign.

Example:

public class UnaryOperatorExample {

    public static void main(String[] args) {

        int a = 5;

        int b = -a; // Negates the value of a

        System.out.println("b = " + b); 

    }

}

Output: 
b = -5

3. Increment (++) #

The increment operator increases the value of the operand by 1. It has two forms:

  • Prefix Increment (++a): Increments the value before using it in an expression.
  • Postfix Increment (a++): Uses the current value in an expression, then increments it.

Example:

public class UnaryOperatorExample {

    public static void main(String[] args) {

        int a = 5;

        // Prefix: a is incremented to 6, then b is assigned 6
        int b = ++a; 

        System.out.println("a = " + a + ", b = " + b); 

        int c = 5;

        // Postfix: d is assigned 5, then c is incremented to 6
        int d = c++; 

        System.out.println("c = " + c + ", d = " + d); 

    }

}

Output: 
a = 6, b = 6

4. Decrement (—) #

The decrement operator decreases the value of the operand by 1. It also has two forms:

  • Prefix Decrement (–a): Decrements the value before using it in an expression.
  • Postfix Decrement (a–): Uses the current value in an expression, then decrements it.

Example:

public class UnaryOperatorExample {

    public static void main(String[] args) {

        int a = 5;

        // Prefix: a is decremented to 4, then b is assigned 4
        int b = --a; 

        System.out.println("a = " + a + ", b = " + b); 

        int c = 5;
 
        // Postfix: d is assigned 5, then c is decremented to 4
        int d = c--; 

        System.out.println("c = " + c + ", d = " + d); 

    }

}

Output: 
a = 4, b = 4
c = 4, d = 5

5. Logical NOT (!) #

The logical NOT operator inverts the boolean value of the operand. If the operand is true, it becomes false, and vice versa.

Example:

public class UnaryOperatorExample {

    public static void main(String[] args) {

        boolean isTrue = true;

        // Inverts the value of isTrue
        boolean isFalse = !isTrue; 

        System.out.println("isTrue = " + isTrue); 

        System.out.println("isFalse = " + isFalse);

    }

}

Output: 
isTrue = true
isFalse = false

Summary #

Unary operators in Java are powerful tools for performing basic operations on single operands, such as changing signs, incrementing/decrementing values, or inverting booleans. They are essential for writing concise and efficient code in various programming scenarios.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
What is Assignment Operators in Java? What is Ternary Operators in Java?
Table of Contents
  • 1. Unary Plus (+)
  • 2. Unary Minus (-)
  • 3. Increment (++)
  • 4. Decrement (--)
  • 5. Logical NOT (!)
  • Summary
© Copyright [2018-2025]. All Rights Reserved.