Java Boolean logicalOr() Method
The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands.
Syntax:
public static boolean logicalOr (boolean a, boolean b)
Parameters:
The parameters ‘a’ and ‘b’ represents first and second operands.
Return Value:
The logicalOr () method returns the result after implementing logical OR operation on the parameters a and b.
- It returns true, if either of the parameter is true.
- It returns false, if and only if both the parameters are false.
Example 1
public class JavaBooleanLogicalOrMethodExample1 {
public static void main(String[] args) {
Boolean b1 = true;
Boolean b2 = false;
// return boolean value after implementing logical OR operator
Boolean b3 = Boolean.logicalOr(b1,b2);
Boolean b4 = Boolean.logicalOr(b1,b1);
Boolean b5 = Boolean.logicalOr(b2,b2);
Boolean b6 = Boolean.logicalOr(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 = true 2.true true = true 3.false false = false 4.false true = true
Example 2
import java.util.Scanner;
public class JavaBooleanLogicalOrMethodExample2 {
public static void main(String[] args) {
Boolean bool1 = true;
Boolean bool2 = false;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the dividend : ");
int dividend =scanner.nextInt();
System.out.print("Enter the divisor : ");
int divisor =scanner.nextInt();
if(dividend<0 ||divisor<=0||divisor> dividend){
bool1 = false;
}
Boolean bool3 =Boolean.logicalOr(bool1,bool2);
if(bool3==true) {
System.out.println("Quotient = "+dividend/divisor +"\nRemainder = "+dividend%divisor);
}
else{
System.out.println("Plz enter a valid dividend and divisor.");
}
}
}
Output
Enter the dividend : 67 Enter the divisor : 0 Plz enter a valid dividend and divisor.
Example 3
import java.util.Scanner;
public class JavaBooleanLogicalOrMethodExample3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a, b;
Boolean b1 = false;
Boolean b2 = false;
System.out.println("Enter two no.s");
a = scanner.nextInt();
b = scanner.nextInt();
if (b>a){
b1=true;
}
if (a == b) {
System.out.println("Both are equal.");
}
else {
boolean b3 = Boolean.logicalOr(b1, b2);
if (b3 == true) {
System.out.println(b + " is greater");
}
else {
System.out.println(a + " is greater");
}
}
}
}
Output
Enter two no.s 56 109 109 is greater
