# boolean operators

vivien siu
Ranch Hand
Posts: 143
hello guys, 2 questions i would like to ask on boolean operations:

1. Is there any operators that can be used with boolean data types?

2. I am trying to do a comparison between 2 boolean type variable to get the result, like this:

if a is true, b is true then c is true,
else c is false.

I know this can be done by using an if/else , how about arithmetic operations or other methods if possible?

Garrett Rowe
Ranch Hand
Posts: 1296
if a is true, b is true then c is true,
else c is false.

[ May 09, 2006: Message edited by: Garrett Rowe ]

marc weber
Sheriff
Posts: 11343
Originally posted by vivien siu:
...how about arithmetic operations or other methods if possible?

Note that in Java, boolean values cannot be treated arithmetically.

Jeff Albertson
Ranch Hand
Posts: 1780
Originally posted by vivien siu:

1. Is there any operators that can be used with boolean data types?

I can only think of 13:

boolean a = x && y;
boolean b = x || y;
boolean c = x & y;
boolean d = x | y;
boolean e = x == y;
boolean f = x != y;
boolean g = x ^ y;
boolean h = (x = y);
boolean i = (x &= y);
boolean j = (x |= y);
boolean k = (x ^= y);

boolean l = !x;
boolean m = x ? y : !y;

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Wonder why they left out &&=. I really wanted it yesterday.

stevie liu
Greenhorn
Posts: 6
how to use '&&='
I think '&&=' can't be used in boolean.

Henry Wong
author
Marshal
Posts: 21190
80
Originally posted by Stan James:
Wonder why they left out &&=. I really wanted it yesterday.

Hmmm... interesting... So, if the left side is true, it will assign it to the right side. Otherwise, it will short circuit the expression. Sort of like this.

Henry

vivien siu
Ranch Hand
Posts: 143
OMG, got such operators like "&&=" ???

How does this work? Does operators like this come out in SCJP exams?

Henry Wong
author
Marshal
Posts: 21190
80
Originally posted by vivien siu:
OMG, got such operators like "&&=" ???

How does this work? Does operators like this come out in SCJP exams?

Don't panic. There is no such operator. If you reread the thread, you'll notice that it was about how that operator is missing...

Henry

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
I just wanted shorthand for

bool = bool && testone();
bool = bool && testtwo();
bool = bool && testthree();

The real test expressions were long enough they did not read well as

bool = testone() && testtwo() && testthree();