Groovy - Logical Operators
Logical operators are used to evaluate Boolean expressions. Following are the logical operators available in Groovy −
| Operator | Description | Example |
|---|---|---|
| && | This is the logical and operator | true && true will give true |
| || | This is the logical or operator | true || true will give true |
| ! | This is the logical not operator | !false will give true |
Example - Usage of AND operator
In this example, we're creating two variables x and y and using logical operators. We've performed a logical AND operation and printed the result.
Example.groovy
class Example {
static void main(String[] args) {
boolean x = true;
boolean y = false;
println("x && y = " + (x && y));
}
}
Output
When we run the above program, we will get the following result.
x && y = false
Example - Usage of OR operator
In this example, we're creating two variables x and y and using logical operators. We've performed a logical OR operation and printed the result.
Example.groovy
class Example {
static void main(String[] args) {
boolean x = true;
boolean y = false;
println("x || y = " + (x || y));
}
}
Output
When we run the above program, we will get the following result.
x || y = true
Example - Usage of Logical NOT operator
In this example, we're creating two variables a and b and using logical operators. We've performed a logical NOT operation and printed the result.
Example.groovy
class Example {
static void main(String[] args) {
boolean x = true;
boolean y = false;
println("!(x && y) = " + !(x && y));
}
}
Output
When we run the above program, we will get the following result.
!(x && y) = true
Advertisements