tags:

views:

217

answers:

3

Is there a reason why there is an option for a Boolean and a bool? I dont seem to see the difference.

+1  A: 

Edit by 280Z28: FOR THE DOWNVOTERS: The question originally specified Java, which makes this answer correct at the time it was posted. /end 280Z28

Boolean can take on the value null, boolean cannot.

vicatcu
huh? huh? huh? huh?
JonH
`Nullable<Boolean>` can take null. `Boolean` cannot.
CesarGon
@JonH/CesarGon: When this answer was made, both Java and C# were in the question tags. This answer is correct for Java, but not C#.
R. Bemrose
+13  A: 

In Java, Boolean is a class and bool is a primitive. Even though Java supports autoboxing this only masks the syntactic inconvenience but does not convey the performance hit involved. In Java, it is important to know the difference between the two and when it matters.

In C#, bool is simply an alias for System.Boolean - they are synonymous and can be used interchangeably. The compiler will actually translate all instances of the bool keyword into System.Boolean in the resulting IL. However, while the compiled types are the same, the designers of C# intended users to express language primitives using their short form names (bool, int, etc.). So the "C# way of doing things" is using bool over System.Boolean.

Andrew Hare
+1 for describing difference between Java and C#; the distinction between bool in the two languages is subtle but important. As for PHP: PHP is not strongly typed, so has neither an explicit *bool* nor *Boolean* type.
BlueRaja - Danny Pflughoeft
+1 for describing the difference. I edited the post to include the recommended choice within C# code.
280Z28
A: 

Java: http://stackoverflow.com/questions/1295170/gwt-java-difference-between-boolean-and-boolean/1295218#1295218

C#: http://stackoverflow.com/questions/134746/what-is-the-difference-between-bool-and-boolean-types-in-c/134771#134771

PHP doesn't have either bool or Boolean, so it is kind of hard to talk about it ;-) In PHP, all non-null, non-'0' expressions are true, and everything else is false… very similar to most "older" languages.

Paul Wagland