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

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

WBOY
Release: 2023-04-20 22:55:21
forward
2460 people have browsed it

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);
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template