Unary Operators in Java
Here are the implementation of Unary Operator in Java language:
class Main {
public static void main(String[] args)
{
int a = 10;
int b = 5;
// Unary increment (++)
// Increment 'a' by 1 before using its value
System.out.println("Unary Increment: " + ++a);
// 'a' has been incremented
System.out.println("a after increment: " + a);
// Unary decrement (--)
// Decrement 'b' by 1 before using its value
System.out.println("Unary Decrement: " + --b);
// 'b' has been decremented
System.out.println("b after decrement: " + b);
// Unary plus (+)
int c = -5;
// The unary plus doesn't change the value of 'c'
System.out.println("Unary Plus: " + (+c));
// Unary minus (-)
// The unary minus negates the value of 'c'
System.out.println("Unary Minus: " + (-c));
// Unary logical NOT (!)
boolean d = false;
// Logical NOT of false is true
System.out.println("Unary Logical NOT: " + !d);
// Unary bitwise NOT (~)
int e = 1;
// Bitwise NOT of 1 is -2
System.out.println("Unary Bitwise NOT: " + ~e);
}
}
Output
Unary Increment: 11 a after increment: 11 Unary Decrement: 4 b after decrement: 4 Unary Plus: -5 Unary Minus: 5 Unary Logical NOT: true Unary Bitwise NOT: -2
Unary Operators in Programming
In programming, operators act as powerful tools for manipulating data and performing various operations. Among these, unary operators stand out, functioning on a single operand to transform or evaluate data in different ways. This post explains the types, implementations, and best practices associated with unary operators across several programming languages.
Table of Content
- What are Unary Operators?
- Types of Unary Operators
- Unary Operators in C
- Unary Operators in C++
- Unary Operators in Java
- Unary Operators in Python
- Unary Operators in C#
- Unary Operators in JavaScript
- Examples and Use Cases of Unary Operator
- Best Practices of Unary Operator
Contact Us