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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-20 22:55:21
forward
2530 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:
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template