Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Logical Complement
Java Variables Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Variables Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java variables and operators.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
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.
LogicalComplementExample.java
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) {
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
}
}
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.