Java Boolean logicalXor() Method

The Boolean.logicalXor() method in Java is used to perform a logical XOR (exclusive OR) operation on two boolean values.

Table of Contents

  1. Introduction
  2. logicalXor() Method Syntax
  3. Examples
    • Applying Logical XOR
    • Using in Conditional Statements
  4. Real-World Use Case
  5. Conclusion


The Boolean.logicalXor() method is a static method in the Boolean class in Java. It returns the result of applying the logical XOR operator to the specified boolean operands. This method is useful for performing logical operations in a clear and concise manner.

logicalXor()() Method Syntax

The syntax for the logicalXor() method is as follows:

public static boolean logicalXor(boolean a, boolean b)
  • a: The first boolean operand.
  • b: The second boolean operand.

The method returns:

  • true if a and b are different.
  • false if a and b are the same.


Applying Logical XOR

The logicalXor() method can be used to perform a logical XOR operation on two boolean values.


public class LogicalXorExample {
    public static void main(String[] args) {
        boolean value1 = true;
        boolean value2 = false;

        boolean result = Boolean.logicalXor(value1, value2);

        System.out.println("Logical XOR result: " + result);


Logical XOR result: true

In this example, since value1 is true and value2 is false, the result of the logical XOR operation is true.

Using in Conditional Statements

The logicalXor() method can be useful in conditional statements for making decisions based on multiple boolean conditions.


public class ConditionalExample {
    public static void main(String[] args) {
        boolean hasKey = true;
        boolean knowsPassword = false;

        if (Boolean.logicalXor(hasKey, knowsPassword)) {
            System.out.println("You can access the safe.");
        } else {
            System.out.println("Access denied.");


You can access the safe.

Handling Multiple Conditions

When dealing with multiple conditions, the logicalXor() method can simplify the logic.


public class MultipleConditionsExample {
    public static void main(String[] args) {
        boolean condition1 = true;
        boolean condition2 = false;
        boolean condition3 = true;

        boolean result = Boolean.logicalXor(condition1, Boolean.logicalXor(condition2, condition3));

        System.out.println("Multiple conditions XOR result: " + result);


Multiple conditions XOR result: false

Real-World Use Case

Toggling Settings

In a real-world scenario, you can use the logicalXor() method to toggle settings based on user input or conditions.


public class ToggleSettingsExample {
    public static void main(String[] args) {
        boolean isDarkMode = true;
        boolean userPreference = false; // User does not prefer dark mode

        boolean shouldEnableDarkMode = Boolean.logicalXor(isDarkMode, userPreference);

        if (shouldEnableDarkMode) {
            System.out.println("Dark mode is enabled.");
        } else {
            System.out.println("Dark mode is disabled.");


Dark mode is disabled.


The Boolean.logicalXor() method in Java is a straightforward way to perform logical XOR operations on boolean values. By understanding how to use this method, you can efficiently manage logical conditions in your Java applications. Whether you are performing simple boolean operations or toggling settings based on user preferences, the logicalXor() method provides a reliable solution for these tasks.