0

Possible Duplicates:
A clear, layman's explanation of the difference between | and || in c# ?
What's the difference between | and || in Java?

what is the difference between the operator | and the operator || ?? and also what is the difference between & and && ??

thanks...

Community
  • 1
  • 1
Ruba
  • 885
  • 4
  • 18
  • 29
  • possible duplicate of [A clear, layman's explanation of the difference between | and || in c# ?](http://stackoverflow.com/questions/684648/a-clear-laymans-explanation-of-the-difference-between-and-in-c) Not about Java, but fully applicable here. – Nikita Rybak Oct 23 '10 at 16:56
  • Also, [What's the difference between | and || in Java?](http://stackoverflow.com/questions/96667/whats-the-difference-between-and-in-java) – Nikita Rybak Oct 23 '10 at 16:58
  • i have read the ~ is not operator ... but i know that ! is the not operator ... can any one help me? – Ruba Oct 23 '10 at 17:08
  • Look at the difference between logical and bitwise operators in general. `~` is just a bitwise 'not' (applied to integer types), while `!` is a logical 'not' operator (applied in boolean expressions). – Nikita Rybak Oct 23 '10 at 17:21

3 Answers3

5

The main difference is that in an expression a && b, b will not be evaluated if a is false, while in a & b both a and b will be evaluated no matter what. One can say that && "short-circuits" the evaluation.

Similarly for a || b: b will not be evaluated if a is true, while in a | b both a and b will be evaluated no matter what.

aioobe
  • 413,195
  • 112
  • 811
  • 826
3

| and & are available for booleans and integer types and don't short-circuit.

&& and || are only available for booleans and they do short-circuit.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
1

Single character | is bitwise OR operation, double || is logical OR

Single character & is bitwise AND operation, double && is logical AND

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541