Difference Between & and && in Java

  In this section, we will discuss the bitwise & and logical && operator in java. we will also explain the key differences between logical and bitwise operators, as well as the use of bitwise & and logical &&.

Bitwise and Logical AND Operators

A single AND operator (&) represents the bitwise AND operator in java. While the double AND operators (&&) are known as Logical AND operators in java.

Bitwise and Logical AND Operators.
Bitwise and Logical AND Operators.

Bitwise AND Operator

A bitwise operator produces a 1 bit if both the operands are also 1. A zero(0) is produced in all other left cases. For example:

										
	10010001  31
& 00110011 51
--------------
00010001 19

Logical AND Operator

A bitwise Logical AND operators are used to control the loops and conditional statements, it always returns true if both conditions are true, it usually returns either true or false. For example:

Difference Between & and &&

A Logical AND evaluates expression1, if it true then it will check the next condition otherwise immediately return a false. After expression check, if all expression is true then Logical AND will return true. The Key Difference between & and && is that short-circuit operation only supported by the Logical AND Operator, Bitwise doesn't support it. evaluations
SN Basis & Operator && Operators
1 Operator Type It is a Bitwise AND operator. It is a Logical AND Operator.
2 Representation Only with Single AND (&). Represent as double AND (&&)
3 Evaluation It compares two bits at a time (left and right on both sides) and returns the appropriate result based on condition. It only evaluates the next condition, if the left side condition is true. If all conditions satisfied then it returns the true otherwise false.
4 Operations Perform bit shift operations. Evaluate the expression then perform bit shift operations.
4 Short-Circuit It doesn't support. A Logical AND support short-circuit.
5 Uses To operate the logical and bitmask operations. To evaluate the logical conditions.
6 Example result = a & b; if( x < y && y < z)

Bitwise and Logical AND Operators Program

The following example illustrates the uses of both bitwise and logical AND operators.

Program Example Of Bitwise AND Operator

In the following example, we use the Bitwise AND operator to perform some bit operation in java.

																				
public class DemoBitwiseAND{

     public static void main(String []args){
        int x=31;  // create a constant x = 31
        int y=51; // create a constant y = 51
       
        int result = x & y; // perform bitwise operation on x and y constants
        
        System.out.println("Bitwise & operation of x and y is "+result);  // display result
               
     
     }
}										
												
										
									

Output of Bitwise AND Operator

										

Bitwise & operation of x and y is 19
											
										
									

Program Example Of Logical AND Operator

In the following example, we use the Logical AND operator in IF condition to print some messages if a condition is either true or false and for both cases.

																						
public class DemoLogicalAND{

     public static void main(String []args){
        int x=5; // create a constant x = 5
        int y=6; // create a constant y = 5
        int z=10; // create a constant z = 5
        
        
        // For Condition True
        if( x < y && y < z){
            
               System.out.println("For Condition True"); // print the result
               
        }
        else{
             System.out.println("For Condition False"); // print the result
        }
        
        // For Condition False
        if( x < y && y > z){
            
               System.out.println("For Condition True"); // print the result
               
        }
        else{
             System.out.println("For Condition False"); // print the result
        }
     }
}					
												
										
									

Output of Logical AND Operator

										
									
For Condition True
For Condition False									
									
												
										
									

Combined program of Logical AND Operator and Bitwise AND operator

In the following example, we use both case Logical AND and Bitwise AND operator, If the Logical AND condition return true then it will perform the bitwise AND operation else it will not perform the bitwise operation.

																						
public class DemoLogicalAndBitwiseAND{

     public static void main(String []args){
        int x=31; // create a constant x = 31  in binary  10010001
        int y=51; // create a constant x = 51  in binary 00110011
        int z=61; // create a constant x = 61
        int result;
        
        // If Condition True then perform the Bitwise operation
        if( x < y && y < z){
               result= x & y;  // result = 19 in binary 00010001 
               System.out.println("Condition Is True and Result of Bitwise operation (x & y) is "+result);
               
        }
        else{
             System.out.println("No bitwise operation performed");
        }
        
        // If Condition False no any operation will perform
        if( x < y && y > z){
               result= x & y;
               System.out.println("Condition Is True and Result of Bitwise operation (x & y) is "+result);
               
        }
        else{
             System.out.println("No bitwise operation performed");
        }
        
       
     }
}					
												
										
									

Output of Logical AND Operator

										
									
Condition Is True and Result of Bitwise operation (x & y) is 19
No bitwise operation performed								
									
												
										
									
Announcement

Advertisement