What the not operator does
The logical not operator, !, flips a Boolean value. true becomes false and false becomes true. You put it in front of the value it flips, so it is a prefix operator that works on one Boolean, unlike && and ||, which join two.
println(7 <= 10) // true
println(!(7 <= 10)) // false
println(12 != 12) // false
println(!(12 != 12)) // true
println(!true) // false
println(!false) // true
The parentheses in !(7 <= 10) matter. Without them, ! would apply to 7 alone, and !7 is a compile error because 7 is not a Boolean. The operator binds tighter than comparisons, so wrap the whole comparison when you want to flip its result.
Using ! with Boolean variables
The operator is most useful with a Boolean variable, because it lets the condition read naturally:
val hungry = true
if (!hungry) {
println("I can wait a little longer")
} else {
println("I need food!")
}
Here hungry is true, so !hungry is false. The if block is skipped, the else block runs, and the program prints I need food!.
Kotlin only accepts a Boolean as the condition of an if. It does not treat numbers or strings as true or false, so if (n) with an Int fails to compile with a message that the inferred type is Int but Boolean was expected. The not operator follows the same rule: you can only apply it to a Boolean.
Ways to write the same condition
Often there is a comparison that says the same thing without !. These pairs are equivalent:
| with ! | without ! |
|---|---|
!(a == b) | a != b |
!(a < b) | a >= b |
!(a <= b) | a > b |
Prefer the version without ! when it is just as clear. Keep ! for Boolean variables and for flipping a whole condition, where a negated name such as !isSunny reads well.
Combining ! with && or || follows De Morgan's laws: !(a && b) equals !a || !b, and !(a || b) equals !a && !b. When you flip a group, every condition inside flips and the operator changes.
A quick check: with val a = true and val b = false, a && b is false, so !(a && b) is true. The other form agrees, since !a || !b is false || true, which is also true. Use whichever form reads more clearly in your code, and prefer the one with fewer negatives when you can.
Negating other Boolean expressions
The operator works on any expression of type Boolean, including function calls. With val name = "", the expression !name.isEmpty() is false, because isEmpty() returns true and ! flips it. Kotlin offers isNotEmpty() for the same check, which reads more directly.
Membership tests have their own negated form. For val age = 40, both age !in 18..30 and !(age in 18..30) are true, and the first is shorter. Use !in for ranges and collections, and != for equality.
Common mistakes
- Forgetting the parentheses.
!x > 5does not mean "not (x > 5)". It tries to negatexfirst, which fails unlessxis aBoolean. Write!(x > 5)or, better,x <= 5. - Double negatives.
!!is a different operator in Kotlin, the non-null assertion, and has nothing to do with flipping twice. Write!(!ready)or justreadyif you ever need a double negation. - Mixing up the branches. With
if (!isSunny), theifblock is for the not-sunny case and theelseblock is for the sunny case.
The lesson's exercise
The code starts with val isSunny: Boolean = true. The task is to print "Learning to code" when it is not sunny, and "Going to the beach" when it is.
- Write the
if. Put!isSunnyin the parentheses. The condition istrueexactly whenisSunnyisfalse. - Print inside it. Call
println("Learning to code")in the block. - Add the
else. Write it on the same line as the brace that closes theif, as} else {. It runs when!isSunnyisfalse, which means it is sunny, and it prints"Going to the beach".
val isSunny: Boolean = true
if (!isSunny) {
println("Learning to code")
} else {
println("Going to the beach")
}
With isSunny set to true, !isSunny is false, so the program prints Going to the beach. Change the value to false and it prints Learning to code.
Key takeaways
!flips a Boolean:!trueisfalseand!falseistrue.- Wrap a whole comparison in parentheses before negating it:
!(7 <= 10). - It works only on
Booleanvalues, andifconditions must beBooleantoo. - When a plain comparison says the same thing, such as
a != b, it is often easier to read.