which statement holds good for ..and why ?

class CheckOperator{ public static void main(String args[]) { boolean b1 =false; boolean b2 =true; }}a)b1 == b2b)b1&b2c)b1 | b2d)b1 || b2
[240 byte] By [chandankalra] at [2007-9-26 1:15:11]
# 1
a) is false (false is not equal to true)b) and c) does not compile (I think) & and | are bit operators and I don't think you can use them with booleans.d) is true (false or true is true)
DanielN at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 2
i think i m any one among c or d could be true java say that ..either op1 or op2 is true, always evaluates op1 and op2 for (c)either op1 or op2 is true, conditionally evaluates op2 for (d)kindly clarify
chandankalra at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 3

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).

DanielN at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 4

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

CChrisB at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 5

& 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

spieler at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 6

>> & 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

CChrisB at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 7

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.

smh3r at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 8
now is see the ints. It is an overloaded operator with different actions for different types.'|'booleans - Full circuit orints = Bit operator
smh3r at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...
# 9

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!

rumartin at 2007-6-29 0:41:16 > top of Java-index,Archived Forums,Java Programming...