Boolean Java

  • A Boolean value is either true or false.
  • A Boolean expression produces a Boolean value (true or false) when evaluated.

Relational operators:

  • Used to test the relationship between 2 variables, expressions, or values. These relational operators are used for comparisons and they evaluate to a Boolean value (true or false).

Equal to (==): Compares two values for equality after converting them to a common type.

  • 5 == ‘5’ // true

  • 5 == 5 // true

Strictly Equal to (===): Compares two values for equality without type conversion.

  • 5 === ‘5’ // false

  • 5 === 5 // true

Not Equal to (!=): Returns true if the operands are not equal after type conversion.

  • 5 != ‘5’ // false

  • 5 != 6 // true

Strictly Not Equal to (!==): Returns true if the operands are not equal and not of the same type.

  • 5 !== ‘5’ // true

  • 5 !== 5 // false

Greater than (>): Returns true if the left operand is greater than the right operand.

  • 5 > 3 // true

  • 3 > 5 // false

Greater than or Equal to (>=): Returns true if the left operand is greater than or equal to the right operand.

  • 5 >= 5 // true

  • 3 >= 5 // false

Less than (<): Returns true if the left operand is less than the right operand.

  • 3 < 5 // true

  • 5 < 3 // false

Less than or Equal to (<=): Returns true if the left operand is less than or equal to the right operand.

  • 5 <= 5 // true

  • 5 <= 3 // false

Example: The legal age to drive in California is 16 years old. How would we write a Boolean expression to check if someone is at least 16 years old?

%%javascript

public class Main {
    public static void main(String[] args) {
        int age = 17;  // Example age
        boolean isEligibleToDrive = age >= 16;
        System.out.println(isEligibleToDrive);  // Output: true if age is 16 or more, false otherwise
    }
}

<IPython.core.display.Javascript object>
  • A company offers free shipping for orders of at least $50. Write a Boolean expression to check if the total order amount qualifies for free shipping.
%%javascript

public class Main {
    public static void main(String[] args) {
        // Example costs of three items
        double cost1 = 60;
        double cost2 = 45;
        double cost3 = 55;
        
        // Check if the average of the costs is at least $50
        boolean qualifiesForFreeShipping = (cost1 + cost2 + cost3) / 3 >= 50;
        System.out.println(qualifiesForFreeShipping);  // Output: true if the average is 50 or more, false otherwise
        
        // Example of checking if an order amount qualifies for free shipping
        double orderAmount = 55;  // Example order amount
        boolean isEligibleForFreeShipping = orderAmount >= 50;
        System.out.println(isEligibleForFreeShipping);  // Output: true if the order amount is 50 or more, false otherwise
    }
}

<IPython.core.display.Javascript object>

Logical operators:

Used to evaluate multiple conditions to produce a single Boolean value.

Logical AND (&&): Returns true if both operands are true.

  • true && true // true

  • true && false // false

Logical OR (   ): Returns true if at least one of the operands is true.
  • true   false // true
  • false   false // false

Logical NOT (!): Returns the opposite of the boolean value.

  • !true // false

  • !false // true

Example: You win the game if you score at least 10 points and have 5 lives left or if you score at least 50 points and have more than 0 lives left. Write the Boolean expression for this scenario.

%%javascript

let score = 12;  // Example score
let lives = 5;   // Example lives

// Boolean expression to check if the player wins the game
let hasWon = (score >= 10 && lives === 5) || (score >= 50 && lives > 0);

console.log(hasWon);  // Output: true if the player meets any of the winning conditions, false otherwise

<IPython.core.display.Javascript object>

Example: Write a Boolean expression to check if the average of height1, height2, and height3 is at least 65 inches.

%%javascript

let height1 = 66;  // Example height in inches
let height2 = 64;  // Example height in inches
let height3 = 65;  // Example height in inches

// Calculate the average and check if it is at least 65 inches
let isAverageHeightAtLeast65 = (height1 + height2 + height3) / 3 >= 65;

console.log(isAverageHeightAtLeast65);  // Output: true if average height is 65 or more, false otherwise

<IPython.core.display.Javascript object>
<script src="https://utteranc.es/client.js"
        repo="manas12709/portfolio_2025"
        issue-term="pathname"
        label="utterances"
        theme="github-light"
        crossorigin="anonymous"
        async>
    </script>