Unary operators help you change or check a single value quickly. They make your code shorter and easier to read.
Unary operators in Java
++variable; // increase by 1 --variable; // decrease by 1 +variable; // positive sign (usually no change) -variable; // negative sign (negates the value) !boolean; // logical NOT (flips true/false)
++ and -- can be used before (prefix) or after (postfix) a variable.
Logical NOT (!) only works with boolean values.
int a = 5; ++a; // a becomes 6
int b = 5; b--; // b becomes 4
boolean lightOn = false; lightOn = !lightOn; // lightOn becomes true
int c = -10; int d = -c; // d becomes 10
This program shows how unary operators ++, --, !, and unary minus work. It prints values before and after applying these operators.
public class UnaryOperatorsDemo { public static void main(String[] args) { int count = 10; System.out.println("Original count: " + count); System.out.println("Using prefix ++: " + (++count)); System.out.println("After prefix ++, count: " + count); System.out.println("Using postfix --: " + (count--)); System.out.println("After postfix --, count: " + count); boolean isActive = false; System.out.println("Original isActive: " + isActive); System.out.println("After !isActive: " + !isActive); int number = -5; System.out.println("Original number: " + number); System.out.println("Unary minus applied: " + (-number)); } }
Prefix ++/-- changes the value before using it in an expression.
Postfix ++/-- changes the value after using it in an expression.
Unary plus (+) usually does not change the value but can be used for clarity.
Unary operators work on one value to change or check it.
Use ++ and -- to add or subtract one quickly.
Use ! to flip true/false values.
Practice
What does the unary operator ++ do in Java?
Solution
Step 1: Understand the meaning of
The++++operator adds 1 to the current value of a variable.Step 2: Compare with other unary operators
--subtracts 1,!flips boolean, and negation changes sign.Final Answer:
Increases the value of a variable by 1 -> Option BQuick Check:
++means add one [OK]
- Confusing ++ with --
- Thinking ++ negates the value
- Mixing ++ with boolean negation !
Which of the following is the correct **postfix** syntax to decrement a variable x by 1 using a unary operator?
int x = 5;
// Your code here
Solution
Step 1: Identify decrement unary operators
Bothx--and--xdecrement by 1, butx-1is an expression, not an operator.Step 2: Choose correct syntax for decrement
x--;correctly decreasesxby 1 after its current use.Final Answer:
x--; -> Option CQuick Check:
Usex--to subtract one [OK]
- Using x-1; which doesn't change x
- Confusing ! operator with decrement
- Writing invalid syntax like x=!1;
What is the output of the following Java code?
int a = 3;
System.out.println(++a);
System.out.println(a++);
System.out.println(a);Solution
Step 1: Evaluate
++a++aincreasesabefore printing, so prints 4 andabecomes 4.Step 2: Evaluate
a++a++prints currenta(4) then increases it to 5.Step 3: Print final value of
After previous steps,aais 5, so prints 5.Final Answer:
4 4 5 -> Option AQuick Check:
Prefix ++ adds before print, postfix ++ adds after print [OK]
- Mixing prefix and postfix effects
- Forgetting that ++ changes the variable
- Assuming no change in variable after print
Find the error in the following code snippet:
boolean flag = true;
flag = !flag++;
System.out.println(flag);Solution
Step 1: Check variable type and operators
flagis boolean;++operator works only on numeric types, not boolean.Step 2: Identify invalid operation
Usingflag++causes a compile-time error because booleans cannot be incremented.Final Answer:
Cannot apply ++ to a boolean variable -> Option DQuick Check:
++ works only on numbers, not booleans [OK]
- Trying to increment boolean values
- Confusing logical NOT ! with increment ++
- Assuming code compiles without error
Given the code below, what will be the final value of count after execution?
int count = 0;
for (int i = 0; i < 3; i++) {
count = count++ + ++count;
}
System.out.println(count);Solution
Step 1: Understand
Binarycount = count++ + ++count;behavior+evaluates left operand first (count++: old value then increment), then right (++count: increment then new value).Step 2: Trace loop iterations
Iteration 1 (count=0):count++value=0 (count=1),++countvalue=2, sum=2 -> count=2.
Iteration 2 (count=2):count++=2 (count=3),++count=4, sum=6 -> count=6.
Iteration 3 (count=6):count++=6 (count=7),++count=8, sum=14 -> count=14.Step 3: Final value after loop
After 3 iterations, count is 14.Final Answer:
14 -> Option AQuick Check:
Left-to-right: postfix old + prefix new [OK]
- Assuming both increments add fully
- Ignoring postfix increment side effects
- Calculating sum without assignment effect
Ready to prove it?
Pass the test and this pattern moves unknown
