Logical short-circuit operators && and ||

In Java, the AND logic symbol && and or logic symbol || has the nature of short-circuit.
According to the judgment requirements of logic, sometimes it is only necessary to judge the left side of the logic symbol

public class DemoBasic01
{
    
    
	public static void main(String[] args)
	{
    
    
		int x,y=10;
//		System.out.println(x=0);
		if(( (x=0)==0 )||( (y=20)==20 )){
    
    
			// (x=0) 表示赋值 表示 0
			// 左边成立,右边逻辑被短路
			System.out.println("y is "+y); // 10
		}

		int a,b=10;
		if(( (a=0)==0 )|( (b=20)==20 ) ){
    
    
			// | 表示按位或,需要两个操作数
			// 左右两边逻辑语句 都执行
			System.out.println("b is "+b); // 20
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_43341057/article/details/104705831