The And (&&) operator is used to perform a logical conjunction. If the expressions on both sides of the And (&&) operator evaluate to true, the And (&&) operation evaluates to true. If either expression is false, the And (&&) operation evaluates to false, as illustrated in the following examples:

Now question is & will do the same, what is the point? Point is to save operation time, if man is not true, quit looking for the bear, in short circuit if first is false, it will quit in short circuit operator(&&);

 

import java.io.*;

// javac operator_shortcircuit_b.java
public class operator_shortcircuit_b
{
public static void main(String args[])throws IOException
{

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
////////////////
int n1 = 0;
int n2 = 0;
String str= "";
try{
System.out.println("---While Loop---");
while(( n1 !=9)&&(n2 !=11) )
{
System.out.print("Please enter number: ");
str = br.readLine();
int n3 = Integer.parseInt(str);
n1 = n3;
System.out.print("Please enter pin: ");
str= br.readLine();
int n4=Integer.parseInt(str);
n2= n4;
System.out.println("You Entered : " + n1 + n2);
}
}//try
catch(NumberFormatException e) { System.out.println("data was blank");}
///
System.out.println();
}//end of main
}//end of clas]
/*
boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false

*/

Case 1. 1st operand is

Data to be entered in a order so that the exit condition matches with && operators. Once 1st operand is valid, it would evaluate the 2nd operand

 

import java.io.*;
// javac operator_shortcircuit.java
public class operator_shortcircuit
{
public static void main(String args[])throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int value1 = 1;
int value2 = 24;
if((value1 == 1) && (value2 == 2)) { System.out.println("value1 is 1 && value2 is 2");}
if((value1 == 1) & (value2 == 2)) { System.out.println("(value1 == 1) & (value2 == 2)");}
if((value1 == 1) && (value2 != 2)) { System.out.println("(value1 == 1) && (value2 != 2) Where value2 = " + value2);}
if((value1 != 1) & (value2 == 24)) { System.out.println("value1 is 1 AND value2 is 2");}
if((value1 == 1) | (value2 == 1)) { System.out.println("(value1 == 1) | (value2 == 1) Where value2 = " + value2);}
if((value1 == 1) || (value2 == 1)) { System.out.println("(value1 == 1) || (value2 == 1) Where value2 = " + value2);}
System.out.println();
}//end of main
}//end of class
/*
boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false
*/