| by Arround The Web | No comments

Java Logical OR

The logical operator in java is utilized to integrate two or more conditions as well as to enhance the examination of the initial condition. There are various logical operators in Java and one of them is the logical OR operator. The logical OR operator used the two vertical bar symbols “||” in the program. It is utilized between the operand which is to be evaluated. The result of the logical OR operator is true when the two or more specified operands have true values on the evaluation of the condition. It is frequently applied to Boolean (logical) values.

Example 1:

The program of the logical OR operator is simply implemented below. It takes two conditions under which verification occurred. When the first condition is met, the logical OR operator does not check for the second condition. Otherwise, both conditions are examined.

In the above Java program, we have constructed the class “ExampleCase1” and introduced the main() method. In the main() method of this program, we have declared the two integer data type variables “X” and “Y” and these variables are set with the integer values. Then, we have an if-else block where we have set the condition for the variable “X” and “Y” in between the logical OR operator. The logical operator here first checks the condition for the variable “X” and returns true when the condition is satisfied. The second condition for variable “Y” is only checked when the first condition returns false. The print statement of if block will execute when one or both conditions are satisfied. The else block will execute upon the false results from the specified condition by the logical OR operator.

It is to be observed that the if block statement is executed in the output. Notice that both the conditions are not satisfied, only the variable “X” condition returns true and the if statement is printed below.

Example 2:

Now, we have an example where the logical OR and logical AND operator is utilized for showing the different functionality among these two operators. The logical AND operator checks both conditions.

In the program of Java shown above, we have established a class “ExampleClass2”. This class is deployed with the main method. Here, we have first used the logical AND operator inside the “system.out.println” method. In between the logical AND operator, we have specified the conditions. The logical AND operator provides the Boolean results when both conditions are examined. The true Boolean results are obtained when both conditions are true. The logical AND operator gives false Boolean results if any of the conditions are not true.

Then, we used the logical OR operator and set this operator in between the conditions. The logical OR operator checked these conditions and generated the results. If the first condition is true, the logical OR operator will not check the other one and returns the true value. It only examines the other condition when the first condition produces false results.

The output of the logical AND operator is displayed first. It returns one true for the first AND operator and a false for the second AND operator because both specified conditions are satisfied. Next, we have logical OR operator output which displays two true results and one false value based on the verification of the condition.

Example 3:

We have another operator which is the bitwise logical OR operator. This operator performs oppositely to the logical OR operator. The bitwise logical OR operator is denoted as a single vertical bar symbol “|”. It evaluates both conditions whether the first condition provides true value or false.

In the program example above, we have created the class “ExampleCase3”. We have called the main() method there. Then, we declared three variables “val1”, “val2” and “val3” of integer data type and initialized all the variables with different numerical values. After that, we performed the bitwise logical OR operator operation. The bitwise logical OR operator is set between the conditions. The condition is applied to the above-declared variables. We have used two bitwise logical OR operators in the print method. The first bitwise logical OR operator checks both conditions and then generates the results. The second bitwise logical OR operator operation also evaluates both conditions but this time we have incremented the value of the variable “val1”. Now, we have updated the value of the variable “val1” because it is also checked by the bitwise logical OR operator.

The conditions of the bitwise logical operator return the true value after being evaluated. Furthermore, the value of the variable “val1” is incremented as it is evaluated by the bitwise logical OR operator.

Example 4:

Next, we have the XOR operator which is used to perform the bitwise operation. The bitwise logical XOR operator returns false when the condition of both conditions is satisfied. The true value is returned when one of the conditions is false and the other condition is true. The evaluation of the XOR operator was performed from left to right order.

In the aforementioned java program, we have a class “ExampleClass5” and inside the class, we have a main() method. Next, we have defined two Boolean data type variables “m” and “n”. The variable “m” has the true value, and the variable “n” has the false value. Then, we have assigned these two variables in between the bitwise XOR operator inside the variable “res” which returns a Boolean value. After that, we changed the values of the variables “m” and “n” and again applied the bitwise XOR operator to them for different outcomes.

It is observed that the output values are true where the “n” is false and “m” is true. The false value is returned where the variable “m” and “n” both have true values, and both have false values.


The article provides the working of the logical operator. The logical OR operator is part of the operators in java. We have performed various implementations of the logical OR operator here. The logical OR operator results depend on the true and false of the specified condition. We have differentiated between the AND operator and OR operators to demonstrate the functionality of each operator. In addition, we have the working of the bitwise logical OR and XOR operators with the example.

Share Button

Source: linuxhint.com

Leave a Reply