Home > 类库下载 > java类库 > What is the difference between & and && in Java?

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

清浅
Release: 2019-04-17 14:34:24
Original
15646 people have browsed it


The difference between & and && in Java is: in the && operation, when the first expression is false, the following expression will not be executed; while the & operation Regardless of whether the first expression is true or not, the latter expression will be executed

In Java, && and & are both logical operators representing "and", which means and. They are all true when the expressions on both sides are true, and the entire operation result is true, otherwise it is false. Next, in the article, we will introduce the differences between the two in detail, which will serve as a reference and I hope it will be helpful to everyone.

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

[Recommended course: Java Tutorial]

The difference between & and &&

& and && are both logical operation symbols, but && is also called a short-circuit operator. Because when the value of the first expression is false, the second expression will not be evaluated;

and & will execute both expressions regardless of whether the first expression is true. In addition, & can also be used as a bitwise operator. When the expressions on both sides of & are not of Boolean type, & represents a bitwise operation.

&& Operation

int i = 0;        
if(i == 3  && ++i > 0 )
 {
 }
System.out.println("i = " + i);
Copy after login

The result is: i = 0 ==>The second expression is not executed

& Operation

 int i = 0;        
 if(i == 3  & ++i > 0 )
  { 
  }
  System.out.println("i = " + i);
Copy after login

The result is: i = 1 ==>The second expression is executed

Summary: The above is all of this article The content is here, I hope it will be helpful to everyone


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:php.cn
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template