This discussion is archived
10 Replies Latest reply: Sep 19, 2007 10:11 AM by 807600

# compare two primitive  boolean

Currently Being Moderated
What is the best approach to compare two boolean either user == to or Create a Boolean object and call equals method ?
miro
• ###### 1. Re: compare two primitive  boolean
Currently Being Moderated
Use the ==
• ###### 2. Re: compare two primitive  boolean
Currently Being Moderated
!= is also useful in some cases.
But feel free to use something like if( a ^ b ) -- works as well and will impress your teacher...
• ###### 3. Re: compare two primitive  boolean
Currently Being Moderated
Michael.Nazarov@sun.com wrote:
!= is also useful in some cases.
Not when comparing for equality!
But feel free to use something like if( a ^ b ) -- works as well and will impress your teacher...
Doesn't work. Won't impress
• ###### 4. Re: compare two primitive  boolean
Currently Being Moderated
georgemc wrote:
But feel free to use something like if( a ^ b ) -- works as well and will impress your teacher...
Doesn't work. Won't impress
Michael.Nazarov@sun.com wrote:
!= is also useful in some cases.
Not when comparing for equality!
Sure it does! It just needs a little help. ;p
``!(a!=b)``
But feel free to use something like if( a ^ b ) -- works as well and will impress your teacher...
Doesn't work. Won't impress
It just needs a little help too!
``!(a ^ b)``
• ###### 5. Re: compare two primitive  boolean
Currently Being Moderated
hunter9000 wrote:
It just needs a little help too!
``!(a ^ b)``
I think it might be a good thing that I'm not a teacher. If I came across that I'd fail you.
• ###### 6. Re: compare two primitive  boolean
Currently Being Moderated
georgemc wrote:
Michael.Nazarov@sun.com wrote:
!= is also useful in some cases.Not when comparing for equality!
But feel free to use something like if( a ^ b ) -- works as well and will impress your teacher...Doesn't work. Won't impress
Equality is nothing but absence of non-equality, so you can check there is no non-equality to ensure there is equality.
Works. Will impress. :)
• ###### 7. Re: compare two primitive  boolean
Currently Being Moderated
I think it might be a good thing that I'm not a teacher. If I came across that I'd fail you.
I think instead of failing someone who did that, I'd probably just give them the next assignment to compare three booleans and watch him figure out why it's a bad idea himself. :)
• ###### 8. Re: compare two primitive  boolean
Currently Being Moderated
I think instead of failing someone who did that, I'd probably just give them the next assignment to compare three booleans and watch him figure out why it's a bad idea himself. :)
{color:#507C9A}What if the next assignment is to take five booleans and test to see if an odd number of them are true:{color}
``if (a ^ b ^ c ^ d ^ e)``
• ###### 9. Re: compare two primitive  boolean
Currently Being Moderated
For the sake of your sanity and the sanity of those who maintain, extend, re-use and mark your code, always use the simplest of the available options (assuming the options are equivalent in all other ways).

In 2 years time when you're trying to add functionality to your old code, you'll curse yourself if it's littered with statements like !(a ^ b). Using == is far clearer.

In terms of efficiency, I suspect == is a far better choice too. It should end up as a simple one-cycle operation as far as your computer is concerned, whereas calling a method will incur significant overhead.

I'm no expert though, so please correct me if I'm wrong.
• ###### 10. Re: compare two primitive  boolean
Currently Being Moderated
{color:SteelBlue}
The !(a^b) was meant as a joke.
{color}

{color:SteelBlue}
Heh, heh. Bitwise humour...
{color}