166

Possible Duplicates:
What's the difference between | and || in Java?
Difference in & and &&

I was just wondering what the difference between & and && is?
A few days I wrote a condition for an if statement the looked something like:

if(x < 50 && x > 0)

However, I changed the && to just & and it showed no errors. What's the difference?


Example: I compiled this simple program:

package anddifferences;

public class Main {

    public static void main(String[] args) {
        int x = 25;
        if(x < 50 && x > 0) {
            System.out.println("OK");
        }

        if(x < 50 & x > 0) {
            System.out.println("Yup");
        }
    }
}

It printed "OK" and "Yup". So does it matter which one I use if they both work?

Community
  • 1
  • 1
Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • 4
    (Not *exactly* the same question, but the difference between `&` and `&&` is the same as the difference between `|` and `||`.) – Stephen C Aug 26 '11 at 03:25
  • here's an older one :P http://stackoverflow.com/questions/4014535/vs-and-vs – Paul Bellora Aug 26 '11 at 03:28
  • 2
    So, I guess the new question is: how would I know whether to choose & vs. &&? I understand && checks the 2nd condition if and only if the first is true (vs. & which checks both automatically), but whats the advantage of choosing one over the other? – Mxyk Aug 26 '11 at 12:00
  • 3
    @Mike Gates this is a handy example if(x!=null && x.length>0)... if we were to use the & instead of && here then in the event of x being null we'd get a NullPointerException, using the && prevents this because if x is null then x.length is never called. – default_avatar Oct 31 '13 at 14:50
  • && is short-circuit. – huuthang Dec 07 '17 at 04:19

4 Answers4

416

& is bitwise. && is logical.

& evaluates both sides of the operation.
&& evaluates the left side of the operation, if it's true, it continues and evaluates the right side.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 28
    Great answer. Much more complete than the "logical and bitwise" which doesn't tell much to someone answering this question. – notbad.jpeg Sep 24 '12 at 18:47
  • 13
    & is not bitwise if the operands are Boolean. Only if they are integral. – user207421 Nov 30 '13 at 09:27
  • 1
    @EJP Actually, `&` *is* bitwise for `boolean` operands. Both sides the operation will still be evaluated. – Jeffrey Nov 30 '13 at 16:06
  • 10
    Jeffrey, 'bitwise' doesn't mean 'evaluates both operands'. It means 'operates on each bit separately'. – user207421 Nov 30 '13 at 20:11
  • @EJP A `boolean` is one bit. – Jeffrey Nov 30 '13 at 22:41
  • 2
    You're arguing with the [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22) here. & is either a bitwise or a logical operator, depending on its operands. Your language suggests that bitwise means that both operands are evaluated. It doesn't. – user207421 Dec 01 '13 at 02:56
  • @EJP JLS [§15.22.2](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.2) Boolean Logical Operators &, ^, and |: `then the type of the *bitwise operator* expression is boolean`. According to the JLS, it is still a bitwise operator. – Jeffrey Dec 01 '13 at 15:54
  • So please explain to me what the heading you've just quoted means. "Boolean logical operators &, ^, and |". – user207421 Dec 01 '13 at 19:19
  • @EJP I believe the JLS is referring to the fact that both operands are `boolean` or `Boolean`. The JLS states that the operator is still a bitwise expression. – Jeffrey Dec 01 '13 at 21:36
  • @Jeffrey: "A boolean is one bit"? To argue semantics, it can be several bytes in memory, depending on the JVM. – John P Feb 26 '14 at 20:10
  • 4
    @JohnP That doesn't change the fact that using `&` or `|` with a `boolean` is considered a bitwise operation. A `boolean` *is* one bit, the fact that its implementation may take up 4 or 8 bytes of memory is somewhat arbitrary. – Jeffrey Feb 26 '14 at 20:47
  • int i = 25; int j = 25; if(i++ < 0 & j++ > 0) System.out.println("OK"); System.out.printf("i = %d ; j = %d",i,j); // This will print i=26; j=25 ... As the first condition is false it will not evaluate the other side. – Nickhil Jun 15 '15 at 20:07
  • @Nickhil Your code, [as it stands](https://ideone.com/ABEoBe), will print `i=26; j=26`. If you [add another `&`](https://ideone.com/TEj116) to your if statement to make it the logical operator, it will print `i=26; j=25` – Jeffrey Jun 16 '15 at 04:16
  • @Jeffrey Yes my bad. I am sorry. && is short circuit so this will skip right hand side evaluation if left hand side is false ( because the result is false irrespective of right side), whereas & will execute both sides. Thanks for correcting – Nickhil Jun 16 '15 at 18:33
79

& is bitwise AND operator comparing bits of each operand.
For example,

int a = 4;
int b = 7;
System.out.println(a & b); // prints 4
//meaning in an 32 bit system
// 00000000 00000000 00000000 00000100
// 00000000 00000000 00000000 00000111
// ===================================
// 00000000 00000000 00000000 00000100


&& is logical AND operator comparing boolean values of operands only. It takes two operands indicating a boolean value and makes a lazy evaluation on them.

suat
  • 4,239
  • 3
  • 28
  • 51
41

&& == logical AND

& = bitwise AND

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
41

'&' performs both tests, while '&&' only performs the 2nd test if the first is also true. This is known as shortcircuiting and may be considered as an optimization. This is especially useful in guarding against nullness(NullPointerException).

if( x != null && x.equals("*BINGO*") {
  then do something with x...
}
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
mP.
  • 18,002
  • 10
  • 71
  • 105
  • 1
    Ummm.... what? What do you mean & performs both tests? It's a bitwise AND operation – Jesus Ramos Aug 26 '11 at 03:21
  • 3
    Shouldn't the first condition be `x != null`? That example still throws NPE when `x` would be `null`. By the way, the average developer usually checks it the other way round: `if ("*BINGO*".equals(x)) {}`. – BalusC Aug 26 '11 at 03:24
  • 2
    @Jesus Ramos: both tests means `&` checks both `x != null` (I'm assuming mP meant !=) and `x.equals("*BINGO*")`. With &&, if `x != null` returns false, there's no way the entire statement would evaluate to true, so the second condition would not be checked, thereby ensuring that you don't get an exception when trying to evaluate the second condition (which is also known as short circuiting, as mentioned) – K Mehta Aug 26 '11 at 03:26
  • @KM: yes thanks for noticing the != typo. – mP. Aug 26 '11 at 10:41
  • @BC i had to think of some simple contrived example and thats about as simple as they come. – mP. Aug 26 '11 at 10:41