The logical NOT operator (!) in Java is used to negate a boolean expression. It returns true if the expression is false and false if the expression is true.
The logical NOT operator in Java is represented by the exclamation mark (!
). It negates the boolean value of its operand. Here's an example:
public class LogicalNotExample {
public static void main(String[] args) {
// Example boolean value
boolean x = true;
// Using logical NOT operator
boolean result = !x;
// Displaying the result
System.out.println("Result of logical NOT: " + result);
}
}
In this example, if x
is initially true
, the logical NOT operation (!x
) will result in false
. If x
is initially false
, the NOT operation will result in true
. Feel free to run this Java program to see the output.