Boolean values: Difference between revisions
(added perl) |
|||
Line 7: | Line 7: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
Java has <tt>true</tt> and <tt>false</tt> keywords, representing the only values of type <tt>boolean</tt>. There are also object wrappers <tt>Boolean.TRUE</tt> and <tt>Boolean.FALSE</tt>, of type <tt>Boolean</tt> which may be un-boxed into <tt>boolean</tt>s (auto-unboxed in Java 1.5+). There are no automatic conversions from any other types into <tt>boolean</tt>, and it is a compile-time error to use any type other than <tt>boolean</tt> or <tt>Boolean</tt> in a place that expects a <tt>boolean</tt> (e.g. if-statement condition, while-statement condition, operand of a logical operator, etc.). |
Java has <tt>true</tt> and <tt>false</tt> keywords, representing the only values of type <tt>boolean</tt>. There are also object wrappers <tt>Boolean.TRUE</tt> and <tt>Boolean.FALSE</tt>, of type <tt>Boolean</tt> which may be un-boxed into <tt>boolean</tt>s (auto-unboxed in Java 1.5+). There are no automatic conversions from any other types into <tt>boolean</tt>, and it is a compile-time error to use any type other than <tt>boolean</tt> or <tt>Boolean</tt> in a place that expects a <tt>boolean</tt> (e.g. if-statement condition, while-statement condition, operand of a logical operator, etc.). |
||
=={{header|Perl}}== |
|||
The values in Perl that are false are: the number 0 (as a number, or as the string <tt>'0'</tt>), the empty string <tt>''</tt>, the empty list <tt>()</tt>, and <tt>undef</tt>. |
|||
Everything else is true. |
|||
Perl has no special "true" or "false" keywords. |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 18:38, 10 July 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Show how to represent the boolean states "true" and "false" in a language. If other objects represent "true" or "false" in conditionals, note it.
BASIC
QuickBasic has no keywords for true and false. Boolean expressions evaluate to 0 when false, and a non-zero value when true. Numbers also work in place of boolean expressions following those rules.
Java
Java has true and false keywords, representing the only values of type boolean. There are also object wrappers Boolean.TRUE and Boolean.FALSE, of type Boolean which may be un-boxed into booleans (auto-unboxed in Java 1.5+). There are no automatic conversions from any other types into boolean, and it is a compile-time error to use any type other than boolean or Boolean in a place that expects a boolean (e.g. if-statement condition, while-statement condition, operand of a logical operator, etc.).
Perl
The values in Perl that are false are: the number 0 (as a number, or as the string '0'), the empty string , the empty list (), and undef.
Everything else is true.
Perl has no special "true" or "false" keywords.
Python
The values in Python that are false are: False
, None
, 0 (including 0 of all numeric types, and any object whose .__nonzero__() method returns false), and empty containers (including empty strings, lists, tuples, dictionaries, sets, etc., and any object whose .__len__() method returns 0).
Everything else is true. Constant True
exists.
Ruby
The only values in Ruby that are false are: false
and nil
. They have synonyms FALSE
and NIL
.
Everything else (including the number 0
and the empty string) is true. Constants true
(and TRUE
) exist.
false
, nil
and true
are singleton instances of classes FalseClass
, NilClass
and TrueClass
respectively.