Logical Complement


Logical Complement

🙋 Need help? Ask an expert now!

The logical complement operator, also known as the NOT operator in Java, is represented by an exclamation mark '!'. This operator changes true values to false and false to true. This operator only works with boolean. Please review the example of the NOT operator and its function.

package exlcode;

public class LogicalComplementExample {

  public static boolean exampleVariableOne = true;
  // The character '!' inverts the boolean value of exampleVariableOne
  public static boolean exampleVariableTwo = !exampleVariableOne;

  public static void main(String[] args) {

The value of the two boolean variables are different because one is the inverse of the other. The NOT operator changes the value of exampleVariableOne from true to false and assigns false to exampleVariableTwo.

Please remember to use brackets to section off the NOT operator so it works as intended. As we keep working in Java, especially with if statements, we will see uses for this operator surface as an alternative for writing long lines of code.

Edit Me on GitHub!

Application Question

Consider the following method:

public static void exampleMethod()
  if(false) {
  if(true) {
  if(!false) {
    System.out.println("Not False");
  if(!true) {
    System.out.println("Not True");

What is printed when we call exampleMethod()?