programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# boolean operators

Ranch Hand
Posts: 143
• Number of slices to send:
Optional 'thank-you' note:
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?

Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:

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

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

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by vivien siu:
...how about arithmetic operations or other methods if possible?

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

Ranch Hand
Posts: 1780
• Number of slices to send:
Optional 'thank-you' note:

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;

(instanceof Sidekick)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
Wonder why they left out &&=. I really wanted it yesterday.

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
how to use '&&='
I think '&&=' can't be used in boolean.

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
OMG, got such operators like "&&=" ???

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

Henry Wong
author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

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)
Posts: 8791
• Number of slices to send:
Optional 'thank-you' note:
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();

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?