1

For the logical operators, the operands must be of the type boolean

Suppose the following code:-

int p,q;

p=1;

q=1;

System.out.println("The result is : "+(p&q));

output

The result is : 1

My question is that in the above code, neither of the two variables is of the type Boolean. Then why this code is not producing error?

Also

System.out.println(" This is an error : "+(!p));

Why this statement is producing an error?

kevin gomes
  • 1,775
  • 5
  • 22
  • 30

3 Answers3

4

Though the symbol used for it looks similar, this is not a boolean operation,
it's a bitwise operation and returns an int, not a boolean. See also:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Note that you used & (bitwise AND) and not && (logical AND).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Note that && cannot be applied to int operands. – Oliver Charlesworth Mar 01 '15 at 09:32
  • @OliverCharlesworth Yes, if tried on int operands the `&&` will generate a compilation error. – peter.petrov Mar 01 '15 at 09:32
  • 3
    `&&` is not logical AND, but *conditional* AND. `&` is both logical and bitwise AND, depending on operand type. – Marko Topolnik Mar 01 '15 at 09:33
  • What if I would have declared the above two variables as Boolean. – kevin gomes Mar 01 '15 at 09:34
  • @MarkoTopolnik OK, this subtle terminological note is taken. – peter.petrov Mar 01 '15 at 09:34
  • @kevingomes Then I think auto-unboxing would have taken place, the `Boolean`s would have been turned into `boolean`s, and then & would have had identical behavior to &&. Try it out. See also Marko's note. – peter.petrov Mar 01 '15 at 09:36
  • It's not just about terminological subtleties. Your answer implies that `&` is *not* logical AND, whereas in fact it is (`true & false` is a legal boolean expression). Also, `a && b` has different semantics from `a & b`. – Marko Topolnik Mar 01 '15 at 09:37
  • @MarkoTopolnik Yes, OK, because its operands are boolean, it produces a boolean. But normally if one wants a "logical operation", one would use: `true && false`, not `true & false`. Right? – peter.petrov Mar 01 '15 at 09:38
  • One would *most of the time* use that, but there are certain cases where you really do want `true & false`. – Marko Topolnik Mar 01 '15 at 09:39
  • @peter.petrov `true && false` and `true & false` both will produce same output. The only difference is that the first one is a case of short circuit logical operator – kevin gomes Mar 01 '15 at 09:41
  • @kevingomes I see, OK :) There we go, I also learned something (something well-forgotten probably). You're absolutely right: http://stackoverflow.com/questions/8759868/java-logical-operator-short-circuiting – peter.petrov Mar 01 '15 at 09:42
2

p&q is bit-wise AND of two integers, not a logical operator.

!p is invalid since there is no ! unary operator on integers. ! is only defined for booleans.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

& is bit-wise AND operator.

Binary AND Operator copies a bit to the result if it exists in both operands

For instance in your case.

p = 1 (int) = 0001 (binary) q = 1 (int) = 0001 (binary)

   0001

 & 0001
---------
   0001

resulting to 0001 = 1 in int.

&& on the other hand is logical operator and requires boolean operands.

Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

Further ! is also logical operator and required boolean operands.

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

For reference, visit this site.

codingenious
  • 8,385
  • 12
  • 60
  • 90