int a = +5;
System.out.println(a); // Output: 5
int a = 5;
System.out.println(-a); // Output: -5
int a = 5;
System.out.println(++a); // Output: 6 (Pre-increment)
System.out.println(a++); // Output: 6 (Post-increment)
System.out.println(a); // Output: 7
int a = 5;
System.out.println(--a); // Output: 4 (Pre-decrement)
System.out.println(a--); // Output: 4 (Post-decrement)
System.out.println(a); // Output: 3
boolean isTrue = true;
System.out.println(!isTrue); // Output: false
public class UnaryOperators
{
public static void main(String[] args)
{
// Initialize variables
int a = 5, b = -10;
boolean isTrue = true;
// Unary Plus (+)
System.out.println("Unary Plus (+a): " + (+a)); // Output: 5
// Unary Minus (-)
System.out.println("Unary Minus (-b): " + (-b)); // Output: 10
// Pre-Increment (++a)
System.out.println("Pre-Increment (++a): " + (++a)); // Output: 6
// Post-Increment (a++)
System.out.println("Post-Increment (a++): " + (a++)); // Output: 6
System.out.println("Value after Post-Increment: " + a); // Output: 7
// Pre-Decrement (--a)
System.out.println("Pre-Decrement (--a): " + (--a)); // Output: 6
// Post-Decrement (a--)
System.out.println("Post-Decrement (a--): " + (a--)); // Output: 6
System.out.println("Value after Post-Decrement: " + a); // Output: 5
// Logical NOT (!)
System.out.println("Logical NOT (!isTrue): " + (!isTrue)); // Output: false
// Bitwise Complement (~)
System.out.println("Bitwise Complement (~a): " + (~a)); // Output: -6
}
}
Unary Plus (+a): 5 Unary Minus (-b): 10 Pre-Increment (++a): 6 Post-Increment (a++): 6 Value after Post-Increment: 7 Pre-Decrement (--a): 6 Post-Decrement (a--): 6 Value after Post-Decrement: 5 Logical NOT (!isTrue): false Bitwise Complement (~a): -6
System.out.println(-5);
outputs -5
.
+
) operator is valid only for numeric types.
+true;
will result in a compilation error because true
is a boolean.
int a = 5;
int result = a++ + 10; // Post-increment: result = 15, a = 6
++
) and decrement (--
) operators must be applied to variables, not constants or expressions.
5++;
or (a + b)++;
is invalid.
!
) with Non-Boolean Types:
!
operator only works with boolean expressions.
!5;
results in a compilation error, but !true;
is valid.
int a = 5, b = 10;
System.out.println(-a + b); // Output: 5 (evaluates as (-a) + b)
int a = 5;
System.out.println(++a + ++a); // Output: 13 (evaluates as (++6) + (++7))
final
variables.
final int a = 5;
a++; // Compilation error
char
types, moving to the next or previous Unicode value.
char c = 'A';
System.out.println(++c); // Output: B
++i
) is slightly more efficient than post-increment (i++
) in loops due to avoiding a temporary variable.
for (int i = 0; i < 10; ++i)
{
System.out.println(i);
}
int a = Integer.MAX_VALUE;
System.out.println(++a); // Output: Integer.MIN_VALUE (overflow)
int a = 5;
int b = (++a * 2) + (--a); // a becomes 6, then 5 again; result = 17
!
operator can invert boolean literals directly.
System.out.println(!true); // Output: false
int[] arr = {1, 2, 3};
arr[0]++;
System.out.println(arr[0]); // Output: 2
!
operators can be chained, effectively toggling the boolean value multiple times.
boolean flag = true;
System.out.println(!!!flag); // Output: false
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.