Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Most operators need two values, like a + b. Today's operators need just one. Can anyone name one?
The minus sign in -5? It works on a single number.
Yes, that's unary minus: it flips the sign. Unary plus exists too, although it leaves the value as it is. What about booleans?
The exclamation mark! !true gives false.
Right, that's logical NOT. The most famous pair are ++ and --, which add one to or subtract one from a variable.
So score++ is a short way of writing score = score + 1?
Exactly, and lives-- takes one away. Next we'll see why the position of the ++ matters.
Let a be 5. If I write int b = ++a;, what are a and b?
a becomes 6, and b is 6 as well.
Good. Now start again with int c = 5; int d = c++;. What are c and d?
c is 6, but d is still 5? The ++ happened after the value was used.
That's it. Prefix changes the variable first and hands back the new value; postfix hands back the old value and changes the variable afterwards.
So if I just write count++; on its own line, it doesn't matter which form I use?
Correct. The difference only shows when the value is used inside a bigger expression, such as an assignment or a println.
Puzzle time: int x = 5; x = x++;. What is x afterwards?
6, surely?
Run it and you get 5. x++ hands back the old value 5, x briefly becomes 6, and then the assignment stores 5 back into x.
That's sneaky! Do these operators work on letters too?
On a char, yes. char c = 'A'; c++; gives 'B', because a char is stored as a number. On a byte, ++ wraps 127 around to -128.
And ! is only for booleans, like paused = !paused to switch between true and false?
Exactly. In Java, !5 will not even compile, because ! accepts only true or false.
unary operators act on a single value, unlike binary operators such as a + b.
change the variable first, then hand back the new value.
hand back the old value first, then change the variable.
x++; and ++x; do exactly the same thing when nothing uses the value.
++ and -- need a numeric variable (char included), while ! accepts only booleans.
keep one increment per statement, and never write x = x++, which leaves x unchanged.
Prefix and postfix side by side, showing the value each expression hands back:
public class PrefixPostfix {
public static void main(String[] args) {
int a = 5;
int b = ++a; // a changes to 6 first, then b gets 6
System.out.println("b = ++a -> a = " + a + ", b = " + b); // a = 6, b = 6
int c = 5;
int d = c++; // d gets the old value 5, then c changes to 6
System.out.println("d = c++ -> c = " + c + ", d = " + d); // c = 6, d = 5
int e = 5;
System.out.println("e-- gives " + e-- + ", then e is " + e); // e-- gives 5, then e is 4
System.out.println("--e gives " + --e + ", and e is " + e); // --e gives 3, and e is 3
}
}Tracing increments inside one expression, and the x = x++ trap:
public class IncrementTraps {
public static void main(String[] args) {
int x = 5;
x = x++; // x++ hands back 5, x becomes 6, then the assignment stores 5 again
System.out.println("x after x = x++ : " + x); // 5
int a = 5;
int result = a++ + ++a; // 5 (a becomes 6) + 7 (a becomes 7)
System.out.println("a++ + ++a = " + result + ", and a is now " + a); // 12, and a is now 7
int count = 5;
count++; // on their own, both forms simply add one
++count;
System.out.println("count after count++ and ++count: " + count); // 7
}
}Unary operators on char, double, byte and int values:
public class UnaryOnTypes {
public static void main(String[] args) {
char letter = 'A';
letter++;
System.out.println("letter after ++ : " + letter); // B
System.out.println("+letter : " + (+letter)); // 66, unary plus gives an int
double price = 9.5;
price--;
System.out.println("price after -- : " + price); // 8.5
byte small = 127;
small++; // compiles: ++ casts back to byte, and 127 wraps around
System.out.println("byte 127 after ++ : " + small); // -128
int temperature = 12;
System.out.println("-temperature : " + (-temperature)); // -12
System.out.println("-(-temperature) : " + (-(-temperature))); // 12
}
}Logical NOT for flipping values, toggling a switch and turning a condition around:
public class LogicalNot {
public static void main(String[] args) {
boolean isRaining = false;
System.out.println("isRaining : " + isRaining); // false
System.out.println("!isRaining : " + !isRaining); // true
System.out.println("!!isRaining : " + !!isRaining); // false
boolean lightOn = false;
lightOn = !lightOn;
System.out.println("after 1st toggle: " + lightOn); // true
lightOn = !lightOn;
System.out.println("after 2nd toggle: " + lightOn); // false
int age = 15;
System.out.println("!(age >= 18) : " + !(age >= 18)); // true
}
}Sign in front, the change comes up front; sign behind, the old value is what you find.
Two clerks run the token display at a bank. Priya the Prefix clerk presses the button first and then calls out the new number. Pavan the Postfix clerk calls out the number on the display and only then presses the button. The display always moves on by one, but the customers hear different numbers.
Please Make Ice-cream During Noon: Plus, Minus, Increment, Decrement, Not, the five unary operators.
C.T.U. and U.T.C. - Change Then Use for prefix (++x), Use Then Change for postfix (x++).
Term
What is a unary operator?
Click to flip
Term
If a is 5, what are a and b after `int b = ++a;`?
Click to flip
Term
If c is 5, what are c and d after `int d = c++;`?
Click to flip
Term
What is x after `int x = 5; x = x++;`?
Click to flip
Term
What does `char c = 'A'; c++;` leave in c?
Click to flip
Term
Which type of value does the ! operator accept?
Click to flip
An operator that works on a single operand, such as -x, x++ or !flag.
The value or variable that an operator acts on.
Adds one to a numeric variable and stores the result back in it.
Subtracts one from a numeric variable and stores the result back in it.
Writing the operator before the variable (++x); the variable changes first and the expression gives the new value.
Writing the operator after the variable (x++); the expression gives the old value and the variable changes afterwards.
Flips a boolean value, so true becomes false and false becomes true.
Flips the sign of a number; if x is 5, then -x is -5.
Unary operators work on a single operand: unary plus (+), unary minus (-), increment (++), decrement (--) and logical NOT (!). Increment and decrement come in prefix and postfix forms, which change a variable in the same way but hand back different values.
A unary operator needs only one value to work on. Unary minus flips the sign of a number, unary plus leaves it unchanged, logical NOT flips a boolean, and the increment and decrement operators add or subtract one from a variable. The central idea of this lesson is the difference between prefix (++x) and postfix (x++): both change the variable by one, but prefix hands back the new value and postfix hands back the old one. The lesson traces expressions step by step, shows which types the operators work on, and explains classic traps such as x = x++.
Most operators, like + in a + b, sit between two operands and are called binary operators. A unary operator needs only one operand:
Increment and decrement do two things at once: they calculate a value and store it back into the variable, so count++; needs no = at all. Java also has a unary bitwise complement, ~, which belongs with the bitwise operators.
Both forms add or subtract one. The difference is the value the expression hands back to whatever is using it:
++x, --x): change first, then use. The expression's value is the new value.x++, x--): use first, then change. The expression's value is the old value.When the operator stands alone as a statement, such as count++; or ++count;, nothing uses the value it hands back, so both forms do exactly the same thing.
Java reads operands from left to right, and each ++ or -- takes effect immediately, before the next operand is read. For int a = 5; int result = a++ + ++a;:
a++ hands back 5, then a becomes 6.++a makes a 7 and hands back 7.result is 5 + 7 = 12, and a ends at 7.Java defines this order exactly, so the answer is always 12. Code like this is still hard to read, so in real programs keep each increment in its own statement.
x = x++; leaves x unchanged. The postfix expression hands back the old value 5, x briefly becomes 6, and then the assignment stores the old value 5 back into x. To add one, write just x++;, ++x; or x += 1;.
++ and -- work on any numeric variable, including double and char: letter++ turns 'A' into 'B', and price-- turns 9.5 into 8.5.5++ and (a + b)++ do not compile, and a final constant cannot be changed.byte and short, b++ includes a hidden cast back to the original type, so byte b = 127; b++; compiles and wraps around to -128, while b = b + 1; does not compile, because b + 1 is an int.+letter is 65.! works only on boolean values: !true is false, !!flag is flag again, and flag = !flag; toggles a switch. Unlike C, Java rejects !5 at compile time.Unary operators bind more tightly than arithmetic, comparison and logical operators. -a * b means (-a) * b, and in !isMember && hasTicket the ! applies to isMember only. To negate a whole comparison, add brackets: !(age >= 18). The complete order of operators is covered in the Operator Precedence lesson.