Why do Java and C# not have implicit conversions to boolean?

Posted by Shaun on Stack Overflow See other posts from Stack Overflow or by Shaun
Published on 2010-06-01T17:06:53Z Indexed on 2010/06/01 17:13 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

Since I started Java it's been very aggravating for me that it doesn't support implicit conversions from numeric types to booleans, so you can't do things like:

if (flags & 0x80) { ... }

instead you have to go through this lunacy:

if ((flags & 0x80) != 0) { ... }

It's the same with null and objects. Every other C-like language I know including JavaScript allows it, so I thought Java was just moronic, but I've just discovered that C# is the same (at least for numbers, don't know about null/objects): http://msdn.microsoft.com/en-us/library/c8f5xwh7(VS.71).aspx

Microsoft changed it on purpose from C++, so why? Clearly I'm missing something. Why change (what I thought was) the most natural thing in the world to make it longer to type? What on Earth is wrong with it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about java