6.3 BOOLEAN TYPE


6.3 BOOLEAN TYPE

A Boolean type[2] is allowed to have only one of two values: true or false. A Boolean is used to express the result of logical operations. For example,

      int x;      int y;      ...      ...      bool b = x == y;      in C++      boolean b = x==y;     in Java 

For another example,

   C++:      bool greater( int a, int b ) { return a > b; }   Java:     boolean greater( int a, int b ) { return a > b; } 

In Java, casting to a boolean type from any other type or casting from a boolean type to any other type is not permitted. Note in particular that, unlike the bool type in C++, boolean values in Java are not integers.

[2]Some of the older C++ compilers do not have bool as a built-in type. However, you can easily create the bool type with the following enumeration:

       enum bool {false, true}; 

Enumerations are discussed further in Chapter 7. Suffice it here to note that this declaration makes the symbols false and true as enumerators of the bool type, in the sense that they represent the complete set of values that objects of type bool are allowed to take.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net