Java Operators - Logical Operators

| Page Views: 63

The logical operators are normally used to create a compound boolean expression.

In java they inlcude:

  1. ! - 'logical not operator'
  2. && - 'logical and operator'
  3. || - 'logical or operator'
  4. ^ - 'exclusive logical or operator'

Logical operators are important when you want to execute a statement based on one or several coditions.

Logical operators can combine multiple conditions to for a compound boolean expression.

Logical operators are also known as boolean operators. They operate on boolean values yet still create a boolean value as a result.

Operator Name Description
|| or logical disjunction
&& and logical conjunction
! not logical negation
^ exclusive or logical exclusion


import java.util.Scanner;
 * MrLogical
public class MrLogical {

    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(;
        System.out.print("Enter Your Age: ");
        int age = input.nextInt();
        System.out.print("Enter Your Gender: ");

        if (age < 5 && gender.equalsIgnoreCase("male"))
            System.out.println("You are baby boy");
        if (age < 5 || gender.equalsIgnoreCase("male"))
            System.out.println("You are a baby or a boy");
        if (age < 5 ^ gender.equalsIgnoreCase("male"))
            System.out.println("You are either a baby or a boy but not both.");
        if (!gender.equalsIgnoreCase("male"))
            System.out.println("You a female");



Enter Your Age: 4
Enter Your Gender: female
You are a baby or a boy
You are either a baby or a boy but not both.
You a female

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post