4

Possible Duplicate:
& vs && and | vs ||

What is the difference between & and &&?

I am getting same output for both & and &&.

Ex:

  1. &&:

    if (true && true){
        System.out.println("------if------");
    } else {
        System.out.println("------else------");
    }
    
  2. &:

    if (true & true){
        System.out.println("------if------");
    } else {
        System.out.println("------else------");
    }
    

Output:

  1. ------if------
  2. ------if------

Can we use & and && operators in our code interchangeably?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Satya
  • 7,886
  • 9
  • 36
  • 40
  • 2
    See http://stackoverflow.com/questions/1795808/and-and-or-in-java-if-statements and http://stackoverflow.com/questions/4014535/vs-and-vs – Nivas Oct 13 '11 at 12:28

8 Answers8

2
  • & - bitwise AND
  • && - logical AND

You've true in both cases because in case of logical AND reason is obvious and in case of bitwise AND - true is interpreted as 1, so 1 & 1 alsays returns 1 which turns out into logical true

sll
  • 59,352
  • 21
  • 103
  • 153
  • `&` works fine for booleans too. The operands are not "interpreted" as integers. – aioobe Oct 13 '11 at 12:27
  • +1 for being the only real right answer here. & and && are actually two different operators. If I recall correctly, the fact that one uses full boolean eval and the other no, is just a side effect. you can use &= but I don't think you can use &&= in java. – BigMike Oct 13 '11 at 12:31
2

&& short circuits the evaluation, while & always evaluates both operands.

This is a duplicate question. I'll try to dig out the link for you...

Community
  • 1
  • 1
aioobe
  • 399,198
  • 105
  • 792
  • 807
2

& will force the evaluation of both the left and right operands whereas && is the short-circuit operator and doesn't check the right operand in obvious cases.

// this will evaluate both method calls even if the result
// of checkSomething() is false
boolean result = checkSomething() & checkSomethingExpensive()

// this will skip evaluation of checkSomethingExpensive() if the result of
// checkSomething() is false
boolean result = checkSomething() && checkSomethingExpensive()
Sanjay T. Sharma
  • 22,282
  • 4
  • 58
  • 71
0

logical AND &&

Java operators

The bitwise & operator performs a bitwise AND operation.

Java operators2

Markus Lausberg
  • 12,040
  • 6
  • 40
  • 66
0

Rule of thumb : use && when dealing with booleans, conditions, ifs, etc. I.e. most of the time. Use & in the rare cases when you need to perform bits operations.

solendil
  • 8,292
  • 4
  • 28
  • 29
0
  • &: operation, boolean true & true == true, integer 1001b & 1110b == 1000b
  • &&: comparison, boolean true & true == true, integer compiler error
Sibbo
  • 3,689
  • 2
  • 23
  • 40
0

That's because the following is evaluated:

I will write it in english semantic for you to understand.

For output 1: Logical AND &&

if true AND true IS TRUE.

For ouptut 2: Bitwise AND &

  • Let x = true (in other words, bit 1)
  • x = x AND true (in other words, x = 1 & 1) Bit is AND'ed
  • if (x IS TRUE) then print the if statement.
Buhake Sindi
  • 85,564
  • 27
  • 164
  • 223
-1

&& is a comparison while & is an operation!

Meister Schnitzel
  • 294
  • 1
  • 4
  • 11