4

I'm just curious. I would like to know if there is a specific reason why the expression

var &= expr

does not behave the same way as

var = var && expr.

It looks like the expression in the first one is being executed regardless of a false value on var.

I'm using Java 6, FYI. This code:

public class Test
{
    protected static String getString()
    {
        return null;
    }

    public static void main(String... args)
    {
        String string = getString();
        boolean test = (string != null);
        test = test && (string.length() > 0);
        System.out.println("First test passed");
        test &= (string.length() > 0);
        System.out.println("Second test passed");
    }
}

Gives me:

First test passed
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Béatrice Cassistat
  • 1,018
  • 12
  • 36
  • 1
    Ref. http://stackoverflow.com/questions/7101992/why-do-we-usually-use-not-what-is-the-difference , http://stackoverflow.com/questions/9264897/reason-for-the-exsistance-of-non-short-circuit-logical-operators , http://stackoverflow.com/questions/4014535/vs-and-vs –  Mar 08 '13 at 22:48

6 Answers6

10

& does not behave like &&1,2

& is an eager bitwise/boolean-And.

&& is a short-circuiting logical-And.


1 The line with &= is equivalent to test = test & (string.length() > 0), which would also fail.

2 See Why do we usually use `||` not `|`, what is the difference? - the answers also cover the use of && and &.

Community
  • 1
  • 1
3

It looks like the expression in the first one is being executed regardless of a false value on var.

That's because the second expression uses short-circuit evaluation, while the first expression does not.

Robert Harvey
  • 173,679
  • 45
  • 326
  • 490
2

They're not the same because & is a bitwise operator and && is a logical operator on booleans.

var &= expr is equivalent to var = var & expr, not var = var && expr.

sellibitze
  • 26,764
  • 3
  • 73
  • 92
alestanis
  • 20,883
  • 4
  • 46
  • 66
0

In your case

test = test && (string.length() > 0); // test = false that why strings.length not evaluated

test &= (string.length() > 0); //calling length on null object that why NPE
sol4me
  • 14,603
  • 5
  • 33
  • 32
-1

& is bitwise AND

&& is logical AND.

true && true = true..

& works on numbers, && works on boolean expressions.

0xFE & 0xFF results in 0xFE.

Aniket Inge
  • 24,753
  • 5
  • 47
  • 78
-1
var &= expr

is equivalent to

var = var & expr

In other words it does a bitwise or

var = var && expr

does a logical or of var and expr

Michael Wiles
  • 20,378
  • 18
  • 70
  • 101
  • Although the answer is right, you still miss the point of short circuit. & works very well with booleans. – MrBackend Feb 26 '14 at 19:01