2

In an interview my interviewer asked me :
What can go wrong if you replace && with & in the following code:

String a=null; if (a!=null && a.length()>10) {...}
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • possible duplicate of [Are the &, |, ^ bitwise operators or logical operators?](http://stackoverflow.com/questions/11597978/are-the-bitwise-operators-or-logical-operators) – Sam Sep 26 '12 at 17:53
  • I already answered the question .. ;-) – Shreyos Adikari Jul 18 '13 at 11:50
  • See also https://stackoverflow.com/questions/12133328/if-statement-checks-for-null-but-still-throws-a-nullpointerexception – Raedwald Sep 13 '19 at 13:50

5 Answers5

15

&& is logical AND operator (executes short-circuit behavior, which means that the second operand is evaluated only if needed)

& is Bitwise AND operator (The bitwise & operator performs a bitwise AND operation).

Because no short-circuit behavior, you will get NullPointerException.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
kosa
  • 65,990
  • 13
  • 130
  • 167
5

A NullPointerException will be thrown as there is no short-circuiting when & is used, meaning the right-hand operand is always evaluated even if the result of the left-hand operand is false. When && is used the right-hand operand is evaluated only if the left-hand operand is true.

The sections 15.22.2. Boolean Logical Operators &, ^, and | and 15.23. Conditional-And Operator && in the Java Language Specification describe this behaviour.

hmjd
  • 120,187
  • 20
  • 207
  • 252
3

Got it :

A single ampersand here would lead to a NullPointerException. As && is logical AND operator and the second operand is evaluated only if needed.

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
2

When you use && it wouldnt evaluate the second expression if the first expression is false. & on the other hand would evaluate both the expressions no matter what.

In your case if you replace '&&' with & it would throw NullPointedException

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

& and && have different precedence in expression evaluation, and, without () surrounding the neighboring expression, you might get some "surprises" in some cases. In particular, && is quite late in the precedence, so you usually don't need () with it unless you're combining the results of a mix of logical expressions. But & is just enough earlier to take you off-guard.

&& is short-circuiting, meaning if the first expression is false then the second expression is not evaluated at all. & does not short-circuit. In the above example this means that with && the second expression does not throw NullPointerException -- often an important feature of &&.

Relevant to C-like languages other than Java: Once you've made it past the above two pitfalls, there is the matter of what the operation actually does. If both expressions being evaluated produce "Boolean" results then the values will promote to either 0x00 or 0x01, and the & and && operators will produce essentially the same answer. But if one of the expressions is not Boolean then you might end up ANDing together 0x01 and 0x02, say, and producing 0x00, even when the && operator would have evaluated to true. (Not specifically a problem for Java since it doesn't allow non-Boolean operands for &&, nor does it allow mixed Boolean/int operands for &.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151