×

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logical
AND operation on the specified Boolean operands.

Syntax:
public static boolean logicalAnd (boolean a, boolean b)

Parameters:
The parameters ‘a’ and ‘b’ represents first and second operands.

Return Value:
The logicalAnd () method returns the result after implementing logical AND operation on the parameters a and b.
? It returns true, if and only if both the parameters are true.
? It returns false, if either of the parameter is false.

Example 1

public class JavaBooleanLogicalAndMethodExample1 {
    public static void main(String[] args) {
        Boolean b1 = true;
        Boolean b2 = false;
    // return boolean value after implementing logical And operator
        Boolean b3 = Boolean.logicalAnd(b1,b2);
        Boolean b4 = Boolean.logicalAnd(b1,b1);
        Boolean b5 = Boolean.logicalAnd(b2,b2);
        Boolean b6 = Boolean.logicalAnd(b2,b1);
        System.out.println("1. "+b1+"  "+b2+" = "+b3);
        System.out.println("2. "+b1+"  "+b1+" = "+b4);
        System.out.println("3. "+b2+"  "+b2+" = "+b5);
        System.out.println("4. "+b2+"  "+b1+" = "+b6);
    }
}

Output

1. true false = false
2. true true = true
3. false false = false
4. false true = false

Example 2

public class JavaBooleanLogicalAndMethodExample2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your Age.");
        Double d1 = scanner.nextDouble();
        Boolean b1 = true;
        Boolean b2 ;
        if (d1 >=18) {
            b2 = true;
            // logicalAnd() method will return true
            Boolean b3 = Boolean.logicalAnd(b1,b2);
            if((b3==true)) {
                System.out.println("You are eligible to vote");
            }
        }
        else{
            System.out.println("Sorry! You are not eligible for voting.");
        }
    }
}

Output

Enter your Age.
18
You are eligible to vote

Example 3

import java.util.Scanner;
public class JavaBooleanLogicalAndMethodExample3 {
    static int j=1;
    public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          Boolean b1 = true;
          Boolean b2 = true;
          System.out.println("Enter a number.");
          int a=scanner.nextInt();
          if (a>15|| a<=0){
              b1=false;
          }
          Boolean b3=Boolean.logicalAnd(b1,b2);
          if (b3) {
              for (int i = 1; i <= 150; i++) {
                  int mul=a*10;
                  if (i%a == 0) {
                      System.out.println(a+" * "+ j++ +" = "+i);
                  }
                  if (i==mul){
                      break;
                  }
              }
          }
          else{
              System.out.println("Sorry ! Number is out of range. Plz try a number btw 1-15 ");
          }
    }
}

Output:

Enter a number.
9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

Related Topics

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.