Important fact:
|
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 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
|
Java's library contains a Boolean ⇒ String conversion method:
|
The output string from toString( ) can then be printed on a terminal !
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 !!!
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....
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