Skip to Content

Java boolean Example

  1. /*
  2.         Java boolean Example
  3.         This Java Example shows how to declare and use Java primitive boolean variable
  4.         inside a java class.
  5. */
  6.  
  7. public class JavaBooleanExample {
  8.  
  9.         public static void main(String[] args) {
  10.                
  11.                 /*
  12.                  * boolean is simple Java type which can have only of two values; true or false.
  13.                  * All rational expressions retrun this type of value.
  14.                  *
  15.                  * Declare boolean varibale as below
  16.                  *
  17.                  * boolean <variable name> = <default value>;
  18.                  *
  19.                  * here assigning default value is optional.
  20.                  */
  21.  
  22.                 boolean b1 = true;
  23.                 boolean b2 = false;
  24.                 boolean b3 = (10 > 2)? true:false;
  25.                
  26.                 System.out.println("Value of boolean variable b1 is :" + b1);
  27.                 System.out.println("Value of boolean variable b2 is :" + b2);
  28.                 System.out.println("Value of boolean variable b3 is :" + b3);            
  29.         }
  30. }
  31.  
  32. /*
  33. Output would be
  34. Value of boolean variable b1 is :true
  35. Value of boolean variable b2 is :false
  36. Value of boolean variable b3 is :true
  37. */