位运算符和单符号 [英] bitwise operator and single ampersand

查看:106
本文介绍了位运算符和单符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/7101992/why-do-we-usually-use-not-what-is-the-difference\">Why我们通常用 || 不是 | ,有什么区别

Possible Duplicate:
Why do we usually use || not |, what is the difference?

我可以使用单一的符号,比如&安培; 而不是位运算符如&放大器;&安培; ?可能会出现什么样的差异,是有清楚地说明这个问题,一个具体的例子?

Can I use single ampersand like & instead of a bitwise operator like &&? What kind of differences may arise and is there a specific example to illustrate this issue clearly?

推荐答案

&安培; 将检查两个条件始终。双&放大器;&安培; 将在第一个条件之后,如果它的值为false停止。使用二是短路的条件检查的方式,如果你真的只需要2 1的条件是真还是假。

The single & will check both conditions always. The double && will stop after the first condition if it evaluates to false. Using two is a way of "short circuiting" the condition check if you truly only need 1 condition of 2 to be true or false.

一个例子可以是:

if (myString != null && myString.equals("testing"))

如果的myString ,第一个条件会失败,并不会刻意去检查它的价值。这是你能避免空指针的一种方法。

If myString is null, the first condition would fail and it would not bother to check the value of it. This is one way you can avoid null pointers.


  • &安培; | 两个操作数一直被运用

  • &放大器;&安培; || 第二个操作数只计算时,有必要
  • with & and ¦ both operands are always evaluated
  • with && and ¦¦ the second operand is only evaluated when it is necessary

下面是一个页面的链接与如何使用它们的pretty很好的解释。

Here's a link to a page with a pretty good explanation of how to use these.

Java程序快速参考操作符

这篇关于位运算符和单符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆