JavaJava Logical Operators

Java Logical Operators

Java Developer Tutorials

Over the past several weeks, we have covered a number of operators in Java, such as those for performing arithmetic as well as comparisons. In this programming tutorial, we will be looking at Java’s logical operators. Logical operators are used for performing operations on one or two literals, variables, or expressions for the purpose of combining into the logical outcome. Typically, the returned value for logical operations is a Boolean that is applied in a program to determine its execution flow.

You can learn more about other types of Java operators in these tutorials:

What Are Java’s Logical Operators?

Java supports the following three logical operators:

  • Logical OR Operator(||)
  • Logical AND Operator(&&)
  • Logical NOT Operator (!)

Of these logical operators, the first two are binary operators that go between two operands, while the last one is a unary operator that goes before its operand. This will all become clearer as we examine each operator in turn and show their syntax.

Java Logical OR Operator (||)

The logical OR operator (||) is a binary operator that operates on two conditional statements positioned on either side, as shown here:

result = first_condition || second_condition

The logical OR operator returns true if at least one of the conditions is true. As such, the logical OR operator will check the second condition only if the first one returns false. However, if the first condition is true, the || operator will return true immediately and not bother to check the second condition. For that reason, some people refer to the logical OR operator as “short circuiting“.

Here is a sample application that utilizes the logical OR operator to check whether or not a driver qualifies for a discount on automobile insurance:

import java.util.Scanner;

public class LogicalOrExample
{
  public static void main(String[] args) 
  {
    Scanner sc = new Scanner(System.in);
    // Get input from user
    System.out.println("What's your age?");
    int age = sc.nextInt();
    System.out.println("How many years have you been driving?");
    int yearsDriving = sc.nextInt();
    System.out.println("How many at-fault accidents have you had in the last 10 years?");
    int accidents = sc.nextInt();
    
    // Using logical OR operator
    if(age > 25 || yearsDriving > 10 || accidents < 2)
      System.out.println("Congratulations! You are eligible for a discount on your auto insurance.");
    else
      System.out.println("Sorry, you are ineligible for an auto insurance discount at this time.");
  }
}

Java Logical AND Operator (&&)

Like the logical OR operator, the logical AND operator (&&) is also a binary operator that operates on two conditional statements positioned on either side. The && operator returns true if both the conditions are true. Java’s logical AND operator syntax is:

result = first_condition && second_condition

In contrast to the logical OR operator, the && operator will not check the second condition if the first condition is false, and only checks the second condition if the first one is true.

The following class asks the user for a number between one and ten and utilizes the logical OR operator to validate that it falls within the acccepted range:

import java.util.Scanner;

public class LogicalAndExample
{
  public static void main(String[] args) 
  {
    // Creating Scanner object to get input from user
    Scanner sc = new Scanner(System.in);
    // Get input from user
    System.out.println("Enter a number from 1 to 10");
    int num = sc.nextInt();
    // Using logical AND operator
    if(num >=1 && num <= 10)
      System.out.println(num + " is a valid choice.");
    else
      System.out.println(num + " is an invalid choice.");
  }
}

Read: Top Online Courses to Learn Java Programming

Java’s Logical NOT Operator (!)

The logical NOT operator (!) is the only unary logical operator. Placed before its operand, the ! operator reverses the logical (Boolean) state of its condition. In other words, if a result of the condition is true then the logical NOT operator will make it as false. Similarly, if the condition is false, it will make it true.

Here’s the syntax of Java’s logical NOT (!) operator:

result = ! condition

Note that the space between the ! and its operand is not required, so !condition is also permissible.

If the following class looks familiar to you, it is because it is almost identical to the LogicalAndExample class. The key difference is that, this time, the logical NOT operator is employed to reverse the num >=1 && num <= 10 expression. As a consequence, the invalid choice message is displayed within the main if block, while the valid choice message now occupies the else block.

import java.util.Scanner;

public class LogicalNotExample
{
  public static void main(String[] args) 
  {
    // Creating Scanner object to get input from user
    Scanner sc = new Scanner(System.in);
    // Get input from user
    System.out.println("Enter a number from 1 to 10");
    int num = sc.nextInt();
    // Using logical NOT operator
    if( ! (num >=1 && num <= 10) )
      System.out.println(num + " is an invalid choice.");
    else
      System.out.println(num + " is a valid choice.");
  }
}

Final Thoughts on Java Logical Operators

In this Java programming tutorial, we explored Java’s logical operators, which are used for evaluating and/or combining one or two operands into a Boolean result. As we saw in the code examples, the returned value for logical operations can be applied in a program to determine its execution flow.

Read more Java programming tutorials and guides to software development.

Latest Posts

Related Stories