×

&& Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two purposes. In one case, it is referred to as a logical - And operator. In the opposite case, it is known as the bitwise - AND operator. These operators are all binary operators. This indicates that each operand has an operand on both its left and right. All of the expression's output can be allocated to a variable. Because these operations deal with primitive types, the programmer does not need to import its class.

The " AND ” operator in Java works based on the correctness of the expressions. That is, it only works when the operators with “ AND ” composition return a true value.

This helps in figuring out the standard conditions required to be followed for the evaluation of a problem.

You can understand the working of “ && ” operator with its truth table.

Exp1 value Exp2 value AND value
True True True
True False False
False True False
False False False

By using AND, the evaluation is done by comparing the two Boolean expressions specified before and after “ && ”. The result will be true only if the return value of both expressions is true. Else false will be returned.

Let us see this with an example,

if (a == b && b > c) 
{
//logic to be executed
}

Consider the above condition where you are checking for the relation between three variables “ a ” , “ b ” , “ c ”. The “ && ” operator will execute both expressions if both are true. Else it will come out of the statement.

Consider if the first expression itself is wrong, then what is the use of evaluating the second expression? This is what the “ && ” operator does. If the first operator itself is wrong, then it will not check for the second expression.

Difference between Bitwise AND ( & ) and Logical AND ( && ) :

The Bitwise AND operator refer to the bitwise AND operator ( & ). It is based on a single bit. It requires two operands. A bit in the result is one if and only if both operand's corresponding bits are 1. The operator's output can be any number.

The logical AND operators are the logical AND operators ( && ). It is mostly found in loops and conditional expressions. It is commonly found in Boolean expressions. “ && ” always produces a 0 or a 1.

Bitwise AND ( & ) operator evaluate both expressions on which it is being applied. It is helpful in operating with Boolean datatypes and as well as bits. It is used to examine the logical state and to block off specific bits like parity bits.

The logical AND ( && ) operator evaluates only the left given expression and is helpful in operating with the only Boolean datatype. It is used to examine only the logical conditions of the expressions.

AndDemo.java:

public class AndDemo  
{  
public static void main ( String args [ ] )   
{  
int abb = 10 , baa = 20 ;  
if ( abb == 10 && baa == 20 )  
{
System.out.println(" true condition given ") ;
}  
else
{  
System.out.println (" false condition given ") ;  
}
int cdd = 78, dcc = 90 ;  
if ( abb > cdd && baa < dcc )  
{
System.out.println (" true condition is given ") ;  
}
else  
{
System.out.println ( " false condition given ") ;  
}


}  


}  

Output :

&& operator in Java

BitwiseAndDemo.java :

import java.io.*;
import java.util.*;


public class BitwiseAndDemo 
{  
public static void main ( String args [ ])  
{  
Scanner sc=new Scanner ( System.in ) ;
System.out.println ( " Enter the value of a : " ) ;
int a = sc.nextInt( ) ;
System.out.println ( " Enter the value of b : " ) ;
int b = sc.nextInt( ) ;
Solution obj=new Solution( ) ;
        obj.bitAnd ( a , b) ;

  
}  
}   


class Solution
{
void bitAnd ( int a , int b )
{
System.out.println ( " bitwise and of the given inputs are : " ) ;
System.out.println ( " a & b = " + ( a & b ) ) ;
}
}

Output:

&& operator in Java

Related Topics

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

&amp;&amp; Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.