Boolean Literals
Use of boolean literals.
Learning Objectives
- Use of two boolean literal reserved words true and false.
Source Code
|
Run Output
Code Understanding
boolean pass=true;
Here the boolean literal true has been stored in boolean variable pass.
boolean topper=false;
Here the boolean literal false has been stored in boolean variable topper.
if(pass) System.out.println(“You have successfully cleared the exams.”);
Boolean variable can be directly checked for truth state in if conditions.
if(topper) System.out.println(“Congrats for being the topper”);
else System.out.println(“With a little more effort you can even become a topper.”);
Both true and false conditions can be examined directly.
Notes
- In java a boolean variable can not be set to 1 and 0 as is the case in many other languages.
- The boolean variable saves on memory as it utilises just 1 bit of memory.
Common Errors
- Sometimes students try to use 1 and 0 in place of true and false conditions. This is not valid in Java.
Suggested Filename(s): BooleanLiterals.java
sunmitra| Created: 13-Mar-2018 | Updated: 21-Mar-2018|