java.lang.Boolean logicalOr(boolean a, boolean b)

Description

The logicalOr(boolean a, boolean b) method of Boolean class returns the result of applying the logical OR operator to the specified boolean operands. For reference here is the truth table in performing a logical OR operator.

First Value Second Value Result
true true true
true false true
false true true
false false false

As a general rule, in performing a logical or operator, the only time that it will result false is when both values are false.

Method Syntax

public static boolean logicalOr(boolean a, boolean b)

Method Argument

Data Type Parameter Description
boolean a the first operand
boolean b the second operand

Method Returns

The logicalOr(boolean a, boolean b) method of Boolean class returns the logical OR of a and b.

Discussion

The logiocalOr method of Boolean class is one of the newest addition as of Java 1.8. This is to take into consideration the instances wherein we have to perform OR operation. So instead of relying on conditional statements, we now have an underlying methods that handle the logical OR operation which shortens and simplifying the development of computer programs.

Java Boolean logicalOr(boolean a, boolean b) Example

Below is a simple java example on the usage of logicalOr() method of Boolean class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * logicalOr(boolean a, boolean b) method of Boolean class.
 */

public class BooleanLogicalOrExample {

	public static void main(String[] args) {
	
		// initialize Scanner object
		Scanner s = new Scanner(System.in);
		
		// ask for user input
		System.out.print("Enter first boolean value:");
		
		// parse the user input into boolean
		boolean firstInput = Boolean.parseBoolean(s.nextLine());
		
		// ask for another user input
		System.out.print("Enter second boolean value:");
		
		// parse the second user input as boolean
		boolean secondInput = Boolean.parseBoolean(s.nextLine());
		 
		/*
		 * get the logical OR result
		 * true | true = true
		 * true | false = true
		 * false | false = false
		 * false | true = true
		 */
		
		boolean result = Boolean.logicalOr(firstInput, secondInput);
		System.out.print(firstInput + " | " + secondInput 
				+ " = " + result );		
		
		// close the scanner object
		s.close();

	}

}

To fully demonstrate the behaviour of logicalOr() method, the program above is designed in such a way that user can enter two boolean values and then the result of logicalOr() method is displayed based on the two user input. On this way, the user can experiment on different values as input.

Sample Output

Below is the sample output when you run the above example.

java Boolean logicalOr() example output