Skip to content

Tinyu01/Checking_values_In_Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Logical Operators

This repository provides examples and documentation for Java's logical operators, focusing on how to evaluate multiple conditions efficiently.

Table of Contents

Introduction

When writing conditional logic in Java, we often need to evaluate multiple conditions at once. This repository demonstrates how to use logical operators effectively instead of relying on nested if statements.

Logical Operators

Binary Operators

Java provides two binary logical operators that work on two conditions:

AND (&&)

  • Returns true only when both conditions are true
  • Returns false when at least one condition is false
  • Evaluates left to right and stops at the first false condition (short-circuit)
if ((thereIsPetrolInCar == true) && (thereIsExtraTime == true) && (cashInHand >= 300)) {
    System.out.println("I will go to Las Vegas");
}

OR (||)

  • Returns true when at least one condition is true
  • Returns false only when all conditions are false
  • Evaluates left to right and stops at the first true condition (short-circuit)
if ((thereIsPetrolInCar == true) || (thereIsExtraTime == true) || (cashInHand >= 300)) {
    System.out.println("I will go to Las Vegas");
}

Unary Operators

NOT (!)

  • Reverses the boolean value of a condition
  • !true becomes false
  • !false becomes true
// These two conditions are equivalent
if (marks > 60) {
    System.out.println("Pass.");
}

if (!(marks <= 60)) {
    System.out.println("Pass.");
}

Multiple Conditions

Using logical operators allows you to test multiple conditions simultaneously, making your code:

  • More readable
  • More concise
  • More efficient through short-circuit evaluation

Short-Circuit Evaluation

The process where evaluation stops as soon as the result is determined:

  • For &&: Stops at the first false since the result will be false
  • For ||: Stops at the first true since the result will be true

This behavior improves performance, especially when later conditions are expensive to compute.

Example Scenarios

Toni's Trip Planning

public class TripPlanner {
    public static void main(String[] args) {
        int dollarsToSpare = 350;
        boolean interestedInLuckAndFun = true;
        boolean interestedInHistory = false;
        
        if (dollarsToSpare > 300) {
            if (interestedInLuckAndFun && !interestedInHistory) {
                System.out.println("I will go to Las Vegas.");
            } else if (!interestedInLuckAndFun && interestedInHistory) {
                System.out.println("I will visit Hoover Dam.");
            } else if (!interestedInLuckAndFun && !interestedInHistory) {
                System.out.println("I will visit Area 51 and observe some aliens.");
            } else {
                System.out.println("Not a valid option.");
            }
        } else {
            System.out.println("I will go to my parent's place. No diversions.");
        }
    }
}

Improvements

See IMPROVEMENTS.md for suggested enhancements to the code examples.

About

Checking values In Java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages