Might be true. I didn't check if what I said about b) and c) was correct.
& and | are bitwise AND and bitwise OR they may be defined for booleans so that both operands are evaluated and then combined.
&& and || are logical AND and logical OR and defined so that if the outcome can be found by just evaluating the first operand the second is skipped (lazy evaluation).
This is easy to check. I wrote the code:
public class TestClass extends Object {
public static void main(String[] args) {
boolean b1 =false;
boolean b2 =true;
System.out.println("b1 == b2 is " + (b1 == b2));
System.out.println("b1&b2 is " + (b1&b2));
System.out.println("b1 | b2 is " + (b1 | b2));
System.out.println("b1 || b2 is " + (b1 || b2));
}
}
The output from this is:
b1 == b2 is false
b1&b2 is false
b1 | b2 is true
b1 || b2 is true
I have to admit that I was surprised at b and c, but I expect that Java treats booleans as single bit values. Interestingly enough, if the values were integers, D wouldn't compile:
public class TestClass extends Object {
public static void main(String[] args) {
int b1 =0;
int b2 =1;
System.out.println("b1 == b2 is " + (b1 == b2));
System.out.println("b1&b2 is " + (b1&b2));
System.out.println("b1 | b2 is " + (b1 | b2));
//System.out.println("b1 || b2 is " + (b1 || b2));
//gives error:
// Error: (20) method ||(int, int) not found in class Business_Test.TestClass.
}
}
This gives
b1 == b2 is false
b1&b2 is 0
b1 | b2 is 1
& and | aren't bit Operators.
They are almost the same as
&& and ||
the only difference is,
& and | always evaluate both operates;
while
&& and || don't have to evaluate the second one
if the result is determined after evaluating the first.
this makes a difference when you don't use variables
but function calls where you do something else but
just returning a value.
see this tutorial page for more info:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/relational.html
HTH
Spieler
>> & and | aren't bit Operators.
Yes they are!
public class TestClass extends Object {
public static void main(String[] args) {
int b1 =9;
int b2 =5;
System.out.println("b1 == b2 is " + (b1 == b2));
System.out.println("b1&b2 is " + (b1&b2));
System.out.println("b1 | b2 is " + (b1 | b2));
}
}
gives the answer
b1 == b2 is false
b1&b2 is 1
b1 | b2 is 13
9 is1001
5 is0101
1001 |
0101
1101 = 13
1001 &
0101
0001 = 1
The Java language defines '|' as a non-short-circuit version of '||'.
( isCondA() || isCondB() )
b is not called if a is true
( isCondA() || isCondB() )
b is called even if a is true. This is usefull is you want the method to run each time a check is made even if it isn't needed. This is used when the method has a side effect, such as a counter, that you want to use.
I'm not sure why the above code displayed '13'. I believe it, but the Java Language Specifications define the '|' operator as a full circuit or check between two conditions.
The fact that boolean values behave as if '|' is a non-short-circuit version of '||' is easy to explain in terms of it being a bitwise operator.
Definitions:
false = 0
true != 0
Truth table for booleans as ints under '|':
false | false = false
false | true = true[e.g. 0 | 1 = 1]
true | false = true[e.g. 1 | 0 = 1]
true | true = true[e.g. 1 | 1 = 1]
As long as a boolean type is convertible to an int, this will work as you described. A bit-wise operator will always evaluate both arguments to arrive at its answer. So, the correct answer is that '|' is a bit-wise OR. We can take advantage of a side-effect of the logic to use it as a non-short-cut logical OR because boolean values can be converted to ints and because the value assignments of true and false are consistent.
It's really a moot point, but I get all flustered when I see someone confuse a side-effect with basic behavior.
Cheers!