Java Boolean logicalAnd() Method
The logicalAnd() method of Java Boolean class returns the result of implementing logical
AND operation on the specified Boolean operands.
Syntax:
public static boolean logicalAnd (boolean a, boolean b)
Parameters:
The parameters ‘a’ and ‘b’ represents first and second operands.
Return Value:
The logicalAnd () method returns the result after implementing logical AND operation on the parameters a and b.
? It returns true, if and only if both the parameters are true.
? It returns false, if either of the parameter is false.
Example 1
public class JavaBooleanLogicalAndMethodExample1 { public static void main(String[] args) { Boolean b1 = true; Boolean b2 = false; // return boolean value after implementing logical And operator Boolean b3 = Boolean.logicalAnd(b1,b2); Boolean b4 = Boolean.logicalAnd(b1,b1); Boolean b5 = Boolean.logicalAnd(b2,b2); Boolean b6 = Boolean.logicalAnd(b2,b1); System.out.println("1. "+b1+" "+b2+" = "+b3); System.out.println("2. "+b1+" "+b1+" = "+b4); System.out.println("3. "+b2+" "+b2+" = "+b5); System.out.println("4. "+b2+" "+b1+" = "+b6); } }
Output
1. true false = false 2. true true = true 3. false false = false 4. false true = false
Example 2
public class JavaBooleanLogicalAndMethodExample2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter your Age."); Double d1 = scanner.nextDouble(); Boolean b1 = true; Boolean b2 ; if (d1 >=18) { b2 = true; // logicalAnd() method will return true Boolean b3 = Boolean.logicalAnd(b1,b2); if((b3==true)) { System.out.println("You are eligible to vote"); } } else{ System.out.println("Sorry! You are not eligible for voting."); } } }
Output
Enter your Age. 18 You are eligible to vote
Example 3
import java.util.Scanner; public class JavaBooleanLogicalAndMethodExample3 { static int j=1; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Boolean b1 = true; Boolean b2 = true; System.out.println("Enter a number."); int a=scanner.nextInt(); if (a>15|| a<=0){ b1=false; } Boolean b3=Boolean.logicalAnd(b1,b2); if (b3) { for (int i = 1; i <= 150; i++) { int mul=a*10; if (i%a == 0) { System.out.println(a+" * "+ j++ +" = "+i); } if (i==mul){ break; } } } else{ System.out.println("Sorry ! Number is out of range. Plz try a number btw 1-15 "); } } }
Output:
Enter a number. 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90