In Java's logical operators, there are so four classes of:&& (short-circuit and),& (with), | (or), | | (short circuit or).
&& and & are both expressed and, the difference is && as long as the first condition is met, the later conditions are no longer judged. & has to judge all the conditions.
"&" Action
The code is as follows:
Public class operatedemo01{
/**
Verify (&) and action
Description: The "and" operation requires all conditions to be judged again
*/
Public Static void Main (String args[]) {
if (ten!= &/0= =0) {
/*10! =10 this condition returns false
10/0==0 This place will throw an exception (java.lang.ArithmeticException:/by zero)
Due to the need to determine all conditions of operation, there will be errors at 10/0
*/
System.out.print ("conditions meet!") ");
}
}
}
Operation Result:
Modify the following code:
"&&" Action:
Public class operatedemo02{
/**
Verifying the "Short circuit and &&" operation
Description: "Short circuit and &&" operation if the first condition is not satisfied then the following conditions will not be judged.
*/
Public Static void Main (String args[]) {
if (ten!= &&/0= =0) {
/*10! =10 this condition returns false
Because the first condition is not satisfied, so the 10/0==0 this condition no longer need to judge
*/
System.out.print ("conditions meet!") ");
}
}
}
Operation Result:
|| (short-circuit or) and | (or) are all "or", the difference is | | As long as the first condition is met, the following conditions are no longer judged, and | All conditions are judged.
Look at the following code:
"| |" Operation
Public class operatedemo03{
/**
Verify the "short-circuit or | |" Operation
Description: "Short Circuit or | |" If the first condition is true then the following conditions are no longer judged
*/
Public Static void Main (String args[]) {
if (ten =Ten| | 0= =0) {
/*10==10 This condition returns true
Because the first condition is satisfied, the condition of 10/0==0 no longer has to be judged.
*/
System.out.print ("conditions meet!") ");
}
}
}
Operation Result:
Modify the following code:
| Operation
Public class operatedemo04{
/**
Verify the OR | action
Description: "or |" All conditions require judgment
*/
Public Static void Main (String args[]) {
if (ten =Ten| 0= =0) {
/*
Although 10==10 returns true but 10/0==0 this condition will throw an exception error
So when the second condition is judged, it throws an exception.
*/
System.out.print ("conditions meet!") ");
}
}
}
Operation Result: