Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Logical Operators in Java | Operators and Conditionals
Introduction to Java

Logical Operators in Java

Swipe to show menu

You learn how Java combines multiple conditions into a single decision using logical operators. Instead of only asking one question at a time, we can now build logic that depends on several conditions working together.

In a real coffee shop system, decisions are rarely simple — whether the system gives a discount, accepts an order, or shows a special message usually depends on more than one factor.

Operators

  • &&AND: returns true only if both conditions are true;
  • ||OR: returns true if at least one condition is true;
  • !NOT: reverses a boolean value — true becomes false and vice versa.

Example

Main.java

Main.java

1234567891011121314151617181920
package com.example; public class Main { public static void main(String[] args) { int orderValue = 8; boolean isDeliveryAvailable = true; boolean isLoyalCustomer = false; boolean isWeekendPromo = true; boolean freeDelivery = isDeliveryAvailable && (orderValue > 10); boolean discount = isLoyalCustomer || isWeekendPromo; boolean showRegularPrice = !isWeekendPromo; System.out.println(freeDelivery); // false System.out.println(discount); // true System.out.println(showRegularPrice); // false } }

The example sets up three conditions about the order state. freeDelivery uses && — both delivery must be available AND the order must exceed 10, but since orderValue is 8, the result is false. discount uses || — even though the customer is not loyal, the weekend promo is active, so the result is true. showRegularPrice uses ! to reverse isWeekendPromo, which gives false.

Logical operators are important because they allow combining multiple rules into a single decision instead of writing separate checks.

question mark

What does the && operator do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Logical Operators in Java

You learn how Java combines multiple conditions into a single decision using logical operators. Instead of only asking one question at a time, we can now build logic that depends on several conditions working together.

In a real coffee shop system, decisions are rarely simple — whether the system gives a discount, accepts an order, or shows a special message usually depends on more than one factor.

Operators

  • &&AND: returns true only if both conditions are true;
  • ||OR: returns true if at least one condition is true;
  • !NOT: reverses a boolean value — true becomes false and vice versa.

Example

Main.java

Main.java

1234567891011121314151617181920
package com.example; public class Main { public static void main(String[] args) { int orderValue = 8; boolean isDeliveryAvailable = true; boolean isLoyalCustomer = false; boolean isWeekendPromo = true; boolean freeDelivery = isDeliveryAvailable && (orderValue > 10); boolean discount = isLoyalCustomer || isWeekendPromo; boolean showRegularPrice = !isWeekendPromo; System.out.println(freeDelivery); // false System.out.println(discount); // true System.out.println(showRegularPrice); // false } }

The example sets up three conditions about the order state. freeDelivery uses && — both delivery must be available AND the order must exceed 10, but since orderValue is 8, the result is false. discount uses || — even though the customer is not loyal, the weekend promo is active, so the result is true. showRegularPrice uses ! to reverse isWeekendPromo, which gives false.

Logical operators are important because they allow combining multiple rules into a single decision instead of writing separate checks.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
some-alt