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

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

清浅
清浅Original
2019-03-29 15:20:5615491browse


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);

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);

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!

Statement:
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