if函数中的android,关于android:java中的if语句中&和&&&有什么区别?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:

Difference in & and &&

if (true && true) {

System.out.println("test if");

}

if (true & true) {

System.out.println("test if");

}

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

&&是逻辑运算符,而&是按位运算符。

看看这个。 与您的问题相同。

stackoverflow.com/questions/7199666/

看看去年的这个问题和答案。 stackoverflow.com/questions/5564410/difference-in-and

&&短路,而&短路。例:

if (methodA() && methodB()) {

...

}

在这种情况下,如果methodA已经返回false,则不会调用methodB。形式上,如果您有表达式a && b,则只有将a评估为true时,才会评估b。

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

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

...

}

如果在此处使用单个&,则即使x是null,也会评估x.getSomeValue(),从而导致异常。

请参见:

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.

&是位操作

&&逻辑上链接两个布尔值

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

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

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

因为它防止了nullPointerException。

测试布尔条件时,请始终使用&&。

&&是逻辑与。

true && true为true,其他所有均为false。

&是按位与。

(10&8)= 8

&&是逻辑AND

因此,您要确保BOTH语句都变为true。

&是按位与运算符

在这里看看:

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

在&陈述中,它将同时检查左侧和右侧陈述。在&&中将仅陈述的一侧检查(如果一侧为真,则不检查另一侧)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值