Start learning free

LESSON 78 OF 348 · CONDITIONAL STATEMENTS

Not Operator in Kotlin: Inverting Booleans with !

Lesson Read the full explanation

The logical not operator (!) is used to invert the value of a boolean condition. For example:

7 <= 10 // Evaluates to true
!(7 <= 10) // Evaluates to false

12 != 12 // Evaluates to false
!(12 != 12) // Evaluates to true

!true // Evaluates to false

It is oftenly used with boolean variables:

val hungry = true
if (!hungry) {
    println("I can wait a little longer")
} else {
    println("I need food!")
}

!hungry evaluates to false, so we enter the else and print "I need food!".

Assignment
Follow the Coding Tutorial and play with the not operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · CONDITIONAL STATEMENTS

    Not Operator in Kotlin, explained

    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 > 5 does not mean "not (x > 5)". It tries to negate x first, which fails unless x is a Boolean. 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 just ready if you ever need a double negation.
    • Mixing up the branches. With if (!isSunny), the if block is for the not-sunny case and the else block 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.

    1. Write the if. Put !isSunny in the parentheses. The condition is true exactly when isSunny is false.
    2. Print inside it. Call println("Learning to code") in the block.
    3. Add the else. Write it on the same line as the brace that closes the if, as } else {. It runs when !isSunny is false, 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: !true is false and !false is true.
    • Wrap a whole comparison in parentheses before negating it: !(7 <= 10).
    • It works only on Boolean values, and if conditions must be Boolean too.
    • When a plain comparison says the same thing, such as a != b, it is often easier to read.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free