Logical Operator ! Tutorial

The Logical ! (not) operator is used to perform logical complement or negation operations on boolean variables, expressions, or method return values. That sort of techobabble makes this operator seem like it must have some amazing super powers. In reality, it simply changes the evaluation of a boolean value or result from true to false or false to true. Notice I stated, "evaluation of boolean value or result". It does not change the value of the variable. That is all the ! logical operator does.

boolean trueVar = true;
boolean falseVar = false;
System.out.println(!trueVar); // displays false
System.out.println(!falseVar); // displays true
System.out.println(trueVar); // displays true - the ! does not change the value of trueVar, just the evaluation

Okay, you get it. So why would such an operator ever be used at all, right? The reality is that the ! operator is rarely used on a regular basis in any sort of a program, but there are many circumstances where it makes 100% complete sense.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Not
C:\Java>cd Not
C:\Java\Not>Notepad Not.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Not {
    public static void main(String args[]) {
        boolean trueVar = true;
        boolean falseVar = false;
        System.out.println(!trueVar); 
        System.out.println(!falseVar);
        System.out.println(trueVar); // the ! does not change the value of trueVar, just the evaluation
        System.out.println();
        
        // video game example
        /* I haven't talked about Java methods yet, 
           so don't be concerned that the following code won't make all that much sense. 
           you will understand the logic on what I talk about in the video.
        */
        String gameTypeSelected="HARD";

        boolean healthy = true;
        double currentLife = 1000.0;
        //.... As the game is being played you take a lot of damage.
        currentLife = 308.76; 
        healthy = isHealthy(currentLife, gameTypeSelected); //Don't worry about understanding this statement right now.

        if(!healthy) {
            // call routines to alter the normal flow of the game
            System.out.println("Blur the Screen");
            //blurScreen();
            System.out.println("Simulate the player staggering");
            //simStagger();
            System.out.println("Make the controller seem choppy");
            //choppyController();
        }
        
    }

    public static boolean isHealthy(double lifeLeft, String gameMode ) {     
        boolean returnVal = true;
        if(gameMode == "HARD") {
            if (lifeLeft <= 400.0) {
                returnVal = false;
            }
        } else if (gameMode == "MEDIUM") {
            if (lifeLeft <= 300.0) {
                returnVal = false;
            }
        } else if(gameMode == "EASY") {
            if (lifeLeft <= 200.0) {
                returnVal = false;
            }
        }
        return returnVal;
    }
}

Now switch back to the command prompt (CMD) and type in javac Not.java and press Enter.
Now type in java Not and press Enter.


C:\Java\Not>javac Not.java
C:\Java\Not>java Not
false
true

Blur the Screen
Simulate the player staggering
Make the controller seem choppy


Final thoughts


Tutorials