Home  >  Article  >  Java  >  What is the difference between & and && in Java?

What is the difference between & and && in Java?

WBOY
WBOYforward
2023-04-20 22:55:212423browse

The difference between & and &&

1. && is not satisfied as long as one condition is different. If the first condition is not satisfied, the subsequent conditions will not be judged. . And & needs to judge all conditions.

Differences in concepts

2. && has the function of short circuit, & can be used as a bit operator.

&& has a short-circuit function, that is, if the first expression is false, the second expression will no longer be evaluated.

& can be used as a bit operator. When the expressions on both sides of the "&" operator are not of boolean type, "&" represents a bitwise AND operation. We usually use 0x0f to perform the & operation with an integer. To get the lowest 4 bits of the integer.

Example

public class demo02{
    public static void main(String[] args) {
       int i=1;
       if(i<5 & i<2){
           System.out.println("逻辑与");//逻辑与
       }
       if (i<5 && i<3){
           System.out.println("逻辑与"); //逻辑与
       }
       i = 234 & 99;
       int a = 234 && 99;//错误
       System.out.println(i);
    }
}

The above is the detailed content of What is the difference between & and && in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete