java java中if语句中&和&&有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12578012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:25:59  来源:igfitidea点击:

What is the difference between & and && in if statement in java?

javaandroid

提问by Hemant Patel

Possible Duplicate:
Difference in & and &&

可能的重复:
& 和 && 的差异

if (true && true) {
    System.out.println("test if");
}

if (true & true) {
    System.out.println("test if");
}

both are give same output.why ?

两者都给出相同的输出。为什么?

回答by Heinzi

&&short-circuits, wheras &doesn't. Example:

&&短路,而&没有。例子:

if (methodA() && methodB()) {
    ...
}

In that case, if methodAalready returns false, methodBis not called. Formally, if you have an expression a && b, bis only evaluated if aevaluated to true.

在这种情况下,如果methodA已经返回 false,methodB则不会被调用。正式地,如果您有表达式a && bb则仅在a计算为 时才计算true

Apart from the obvious performance benefit, this is particularly useful for nullchecks:

除了明显的性能优势外,这对于null检查特别有用:

if (x != null && x.getSomeValue()) {
    ...
}

If you used a s single &here, x.getSomeValue()would be evaluated even if xwere null, resulting in an exception.

如果您使用单一&这里,x.getSomeValue()将即使进行评估xnull,导致异常。

回答by Denys Séguret

In the first case, you don't test the second part of the if : the &&operator executes from left to right and stops at the first falsevalue.

在第一种情况下,您不测试 if 的第二部分:&&运算符从左到右执行并在第一个false值处停止。

This may be useful in this case :

在这种情况下这可能很有用:

if (a!=null && a.doSomething()==23) {

because it prevents a nullPointerException.

因为它可以防止 nullPointerException。

When you test boolean conditions, always use &&.

当您测试布尔条件时,请始终使用&&.

回答by Kai

回答by dbalakirev

Please see: http://www.jguru.com/faq/view.jsp?EID=16530

请参阅:http: //www.jguru.com/faq/view.jsp?EID=16530

It depends on the type of the arguments...

For integer arguments, the single ampersand ("&")is the "bit-wise AND" operator. The double ampersand ("&&") is not defined for anything but two boolean arguments.

For boolean arguments, the single ampersand constitutes the (unconditional) "logical AND" operator while the double ampersand ("&&") is the "conditional logical AND" operator. That is to say that the single ampersand always evaluates both arguments whereas the double ampersand will only evaluate the second argument if the first argument is true.

For all other argument types and combinations, a compile-time error should occur.

这取决于参数的类型......

对于整数参数,单个与号 ("&") 是“按位与”运算符。双与号(“&&”)不是为两个布尔参数定义的。

对于布尔参数,单与号构成(无条件)“逻辑与”运算符,而双与号(“&&”)是“条件逻辑与”运算符。也就是说,单与号总是评估两个参数,而双与号只会在第一个参数为真时评估第二个参数。

对于所有其他参数类型和组合,应该发生编译时错误。

回答by Stefan

&& is the logical AND

&& 是逻辑与

so you want to make sure BOTH statements become true.

所以你要确保这两个陈述都成立。

& is the bitwise AND operator

& 是按位与运算符

Have a look here:

看看这里:

http://www.javaforstudents.co.uk/TrueFalse

http://www.javaforstudents.co.uk/TrueFalse

回答by Ramindu Weeraman

In & statetment it will check both left side and right side statements.in && will check only one side of statement(if one side is true it will not check other side)

在 & 语句中,它将检查左侧和右侧的语句。在 && 中将只检查语句的一侧(如果一侧为真,则不会检查另一侧)

回答by Rahul Baradia

&& is logical and. true && true is true and everything else is false. & is bitwise and. (10 & 8) = 8

&& 是逻辑与。true && true 是真的,其他的都是假的。& 是按位与。(10 & 8) = 8