Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean operators

 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wonder why they left out &&=. I really wanted it yesterday.
 
stevie liu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to use '&&='
I think '&&=' can't be used in boolean.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG, got such operators like "&&=" ???

How does this work? Does operators like this come out in SCJP exams?
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic