The logical AND operator (&&) in Java is used to perform a logical AND operation between two boolean expressions. It returns true if both expressions are true and false otherwise.
The logical AND operator in Java is represented by &&
. Here's an example program demonstrating the use of the logical AND operator:
public class LogicalAndExample {
public static void main(String[] args) {
// Example variables
boolean condition1 = true;
boolean condition2 = false;
// Using logical AND operator
boolean result = condition1 && condition2;
// Displaying the result
System.out.println("Result of logical AND: " + result);
// Additional example
int number = 5;
// Checking if the number is between 1 and 10 using logical AND
boolean isBetweenOneAndTen = (number > 1) && (number < 10);
// Displaying the result
System.out.println("Is the number between 1 and 10? " + isBetweenOneAndTen);
}
}
In this example, the program checks two boolean conditions using the logical AND operator (&&
). It also demonstrates using the logical AND operator to check if a number is between 1 and 10. The result is then displayed on the console.