Boolean Literals – Computer Sir Ki Class


Code Learning#JAVA#3822    siteicon   siteicon  

Boolean Literals

Use of boolean literals.

Learning Objectives

  • Use of two boolean literal reserved words true and false.

Source Code

TC++ #3822

Source Code

class BooleanLiterals
public static void main(String[] args)
boolean pass=true;
boolean topper=false;
System.out.println("You have successfully cleared the exams.");
System.out.println("Congrats for being the topper");
System.out.println("With a little more effort you can even become a topper.");

Test it !

Run Output

You have successfully cleared the exams.
With a little more effort you can even become a topper.

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.

Code 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):


sunmitra| Created: 13-Mar-2018 | Updated: 13-Mar-2018|