Java Boolean Logic

MamtaWardhani's avatar
Published Jan 19, 2024Updated Jun 24, 2025
Contribute to Docs

The boolean data type is one of the eight primitive data types in Java and is used to represent logical values. A boolean variable can hold only two possible values: true or false. It is commonly used to represent binary states such as on/off, yes/no, or enabled/disabled.

Building on this, boolean logic in Java refers to the use of boolean values in combination with logical operators like && (AND), || (OR), and ! (NOT) to evaluate conditions and control program behavior. This logic forms the backbone of decision-making in programming—guiding how a program responds to different inputs or situations. Boolean logic is widely used in conditional statements, loops, and validations, with practical applications such as form validation, user authentication, feature toggles, and game state control.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

boolean variableName = value;

Parameters:

  • variableName: The name of the boolean variable following Java naming conventions
  • value: Either true or false (case-sensitive)

Return value:

A boolean data type does not return a value but stores a logical state that can be evaluated in expressions.

Boolean Logical Operators

Boolean values can be combined and manipulated using logical operators:

  • && (AND): Returns true if both operands are true
  • || (OR): Returns true if at least one operand is true
  • ! (NOT): Returns the opposite boolean value
  • ^ (XOR): Returns true if operands have different values

Boolean Logical Methods

The Boolean wrapper class provides several useful methods for working with boolean values:

  • Boolean.parseBoolean(String s): Converts a string to a boolean value
  • Boolean.toString(boolean b): Converts a boolean to its string representation
  • Boolean.valueOf(boolean b): Returns a Boolean object representing the specified boolean value
  • Boolean.compare(boolean x, boolean y): Compares two boolean values

Example 1: Basic Boolean Declaration

This example demonstrates the fundamental way to declare and use boolean variables in Java:

public class BooleanBasics {
public static void main(String[] args) {
// Declare boolean variables with initial values
boolean isJavaFun = true;
boolean isTestPassed = false;
// Display the values
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Did the test pass? " + isTestPassed);
// Boolean values from expressions
boolean isPositive = (10 > 0);
boolean isEqual = (5 == 3);
System.out.println("Is 10 positive? " + isPositive);
System.out.println("Is 5 equal to 3? " + isEqual);
}
}

The output of this code is:

Is Java fun? true
Did the test pass? false
Is 10 positive? true
Is 5 equal to 3? false

This example shows how boolean variables can be declared with literal values (true/false) or assigned the result of boolean expressions. The comparison operators automatically return boolean values.

Example 2: User Login Validation

This example demonstrates a real-world scenario where booleans are used to validate user credentials and control access to system features.

import java.util.Scanner;
public class UserLoginSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Predefined user credentials
String validUsername = "admin";
String validPassword = "password123";
System.out.print("Enter username: ");
String inputUsername = scanner.nextLine();
System.out.print("Enter password: ");
String inputPassword = scanner.nextLine();
// Boolean validation
boolean isUsernameValid = inputUsername.equals(validUsername);
boolean isPasswordValid = inputPassword.equals(validPassword);
boolean isLoginSuccessful = isUsernameValid && isPasswordValid;
// Display results
System.out.println("Username valid: " + isUsernameValid);
System.out.println("Password valid: " + isPasswordValid);
if (isLoginSuccessful) {
System.out.println("Login successful! Welcome to the system.");
} else {
System.out.println("Login failed. Please check your credentials.");
}
scanner.close();
}
}

The output (with correct credentials) is as follows:

Enter username: admin
Enter password: password123
Username valid: true
Password valid: true
Login successful! Welcome to the system.

Note: The output will vary depending on the username and password entered by the user during execution.

This example illustrates how booleans are used in authentication systems to validate user input and control access. The logical AND operator (&&) ensures that both conditions must be true for a successful login.

Example 3: Shopping Cart Discount Calculator

This example shows how booleans can be used in e-commerce applications to determine eligibility for discounts and special offers:

public class ShoppingCartDiscount {
public static void main(String[] args) {
// Customer information
double totalAmount = 120.50;
boolean isPremiumMember = true;
boolean hasPromoCode = false;
int itemCount = 5;
// Discount eligibility conditions
boolean qualifiesForBulkDiscount = itemCount >= 5;
boolean qualifiesForMemberDiscount = isPremiumMember;
boolean qualifiesForPromoDiscount = hasPromoCode;
boolean qualifiesForMinimumPurchase = totalAmount >= 100.0;
// Determine applicable discounts
boolean canApplyBulkDiscount = qualifiesForBulkDiscount && qualifiesForMinimumPurchase;
boolean canApplyMemberDiscount = qualifiesForMemberDiscount;
boolean canApplyPromoDiscount = qualifiesForPromoDiscount;
// Calculate final discount
double discountPercentage = 0.0;
if (canApplyBulkDiscount) {
discountPercentage += 10.0; // 10% bulk discount
}
if (canApplyMemberDiscount) {
discountPercentage += 5.0; // 5% member discount
}
if (canApplyPromoDiscount) {
discountPercentage += 15.0; // 15% promo discount
}
double discountAmount = totalAmount * (discountPercentage / 100);
double finalAmount = totalAmount - discountAmount;
// Display results
System.out.println("=== Shopping Cart Summary ===");
System.out.println("Original Amount: $" + totalAmount);
System.out.println("Premium Member: " + isPremiumMember);
System.out.println("Has Promo Code: " + hasPromoCode);
System.out.println("Item Count: " + itemCount);
System.out.println();
System.out.println("Discount Eligibility:");
System.out.println("Bulk Discount (5+ items, $100+ purchase): " + canApplyBulkDiscount);
System.out.println("Member Discount: " + canApplyMemberDiscount);
System.out.println("Promo Discount: " + canApplyPromoDiscount);
System.out.println();
System.out.println("Total Discount: " + discountPercentage + "%");
System.out.println("Discount Amount: $" + String.format("%.2f", discountAmount));
System.out.println("Final Amount: $" + String.format("%.2f", finalAmount));
}
}

The output of this code is:

=== Shopping Cart Summary ===
Original Amount: $120.5
Premium Member: true
Has Promo Code: false
Item Count: 5
Discount Eligibility:
Bulk Discount (5+ items, $100+ purchase): true
Member Discount: true
Promo Discount: false
Total Discount: 15.0%
Discount Amount: $18.08
Final Amount: $102.43

This example demonstrates how booleans are used in business logic to evaluate multiple conditions and determine eligibility for various discounts. The boolean variables make the code readable and maintainable while handling complex business rules.

Frequently Asked Questions

1. Can boolean variables be assigned integer values like 1 or 0?

No, unlike some programming languages, Java boolean variables can only be assigned true or false values. Attempting to assign integer values like 1 or 0 will result in a compilation error. Java maintains strict type safety for boolean values.

2. What is the default value of a boolean variable?

For instance, for variables and static variables, the default value is false. However, local variables must be explicitly initialized before use, as they do not have default values.

3. Can boolean values be used in arithmetic operations?

No, boolean values cannot be directly used in arithmetic operations like addition or multiplication. Java does not perform automatic conversion between boolean and numeric types. To perform calculations with boolean values, you must first convert them explicitly.

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours