The boolean value output problem

Important fact:

  • The terminal (output device) can only display symbols corresponding to ASCII (or Unicode) codes

Therefore, when you "print" the boolean value 00000001 (true) (by sending 00000001 directly to the terminal), it will display a strange symbol:

    ASCII code 00000001  mean: "Start of Heading"

    The ASCII code 00000001 is used to signal 
    the start (beginning) of a transmitted message  
    in computer communication !!!

    The ASCII code 00000001 is an 
    "unprintable" character !!!   
   

The boolean value output problem explained graphically

The boolean representation for true (00000001) and false (00000000) are unprintable ASCII codes:

The terminal cannot display the boolean codes for true and false directly !!!

DEMO: /home/cs255001/demo/java/Unprintable.java

Solution ?

  • We need to convert the input representation (boolean values) to a printable ASCII String representation

    (Converting boolean values to strings is easy because there are only 2 possible input Boolean values....)

Java's library contains a Boolean ⇒ String conversion method:

  • String toString( boolean b ) in the Boolean class: click here

    The toString( ) method converts an input boolean value 00000000 into the string "false"

    And the toString( ) method converts an input boolean value 00000001 into the string "true"

The output string from toString( ) can then be printed on a terminal !

The solution of the boolean value output problem explained graphically

The boolean representation for true (00000001) and false (00000000) are converted to appropriate ASCII codes:

The terminal can now display the ASCII codes for true and false directly !!!

The toString( ) conversion method
 

Description of the toString( ) method:

  String toString( boolean x ):

       return a String that represents 
       the boolean value x

       If x == true (00000001), we return the string "true"
       If x == false(00000000), we return the string "false"     
   

 

The toString( ) to convert boolean representation into their appropriate strings is also pretty easy to write....

The toString( ) conversion method
 

How is the method toString( ) implementated in Java:

  public static boolean  toString( boolean x )            
  {
     if ( x == true )
        return "true";    // This is a String (= 4 ASCII codes !)
     else
        return "false";   // This is a String (= 5 ASCII codes !)  
  }
   

 

DEMO: /home/cs255001/demo/java/Bool2String.java