×

&& 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

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.