Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference
In Java, unary operators are a type of operator that operate on a single operand to produce a new value. They are fundamental for performing basic operations such as negation, incrementing, decrementing, and logical complement. Here’s a detailed look at the different unary operators available in Java:

1. Unary Plus (+)

  • Description: The unary plus operator is used to indicate a positive value. It’s largely redundant since numbers are positive by default unless specified otherwise.
  • Syntax: +operand
  • Example:
				
					int a = +5;  // a is 5

				
			

2. Unary Minus (-)

  • Description: The unary minus operator negates an expression, converting a positive value to negative, and vice versa.
  • Syntax: -operand
  • Example:
				
					int a = 5;
int b = -a;  // b is -5

				
			

3. Increment Operator (++)

  • Description: The increment operator increases the value of an integer by 1. It has two forms: prefix and postfix.
    • Prefix: ++operand – Increments the value before using it in an expression.
    • Postfix: operand++ – Uses the current value in an expression and then increments it.
  • Example:
				
					int a = 5;
int b = ++a;  // a is 6, b is 6
int c = a++;  // a is 7, c is 6

				
			

4. Decrement Operator (--)

  • Description: The decrement operator decreases the value of an integer by 1. It also has prefix and postfix forms.
    • Prefix: --operand – Decrements the value before using it in an expression.
    • Postfix: operand-- – Uses the current value in an expression and then decrements it.
  • Example:
				
					int a = 5;
int b = --a;  // a is 4, b is 4
int c = a--;  // a is 3, c is 4

				
			

5. Logical Complement Operator (!)

  • Description: The logical complement operator inverts the value of a boolean expression, turning true to false and vice versa.
  • Syntax: !operand
  • Example
				
					boolean flag = true;
boolean notFlag = !flag;  // notFlag is false

				
			

6. Bitwise Complement Operator (~)

  • Description: The bitwise complement operator inverts all the bits of an integer.
  • Syntax: ~operand
  • Example:
				
					int a = 5;          // 00000000 00000000 00000000 00000101 in binary
int b = ~a;         // 11111111 11111111 11111111 11111010 in binary, which is -6 in decimal (two's complement representation)

				
			

Usage Considerations

  • Unary Plus/Minus: Mostly used for indicating the sign of a number.
  • Increment/Decrement: Commonly used in loops and iterative statements.
  • Logical Complement: Essential for boolean logic operations, particularly in conditionals.
  • Bitwise Complement: Useful in low-level programming, bit manipulation, and implementing certain algorithms.

Practical Examples

				
					public class UnaryOperatorsExample {
    public static void main(String[] args) {
        int x = 10;
        int y = -x; // Unary minus
        System.out.println("y = " + y); // Output: y = -10

        int z = 10;
        z++; // Postfix increment
        System.out.println("z = " + z); // Output: z = 11

        int a = 10;
        ++a; // Prefix increment
        System.out.println("a = " + a); // Output: a = 11

        boolean flag = false;
        flag = !flag; // Logical complement
        System.out.println("flag = " + flag); // Output: flag = true

        int b = 5;
        b = ~b; // Bitwise complement
        System.out.println("b = " + b); // Output: b = -6
    }
}

				
			

In summary, unary operators in Java are simple yet powerful tools that enable developers to perform a variety of basic operations with minimal syntax. Understanding these operators is crucial for writing efficient and concise code.

Scroll to Top