Menu
Topics Index
...
`

Boolean - Java Example Program


Boolean
class BooleanTest
{
    public static void main(String arg[])
    {
        Boolean a = new Boolean(true);
        Boolean b = new Boolean(false);
        Boolean c = new Boolean(false);
        
        System.out.println("Boolean value of a : " + a.booleanValue());
        System.out.println("Comparison of b and c using compare : " + Boolean.compare(b, c));
        System.out.println("CompareTo of a and c : " + a.compareTo(c)); // LINE A
        System.out.println("Equals of b and c : " + b.equals(c));
        
        System.setProperty("Isboolean", "true"); // LINE B
        System.out.println("Get Boolean from Isboolean : " + Boolean.getBoolean("Isboolean"));
        
        System.out.println("Hash code for Boolean : " + a.hashCode()); // LINE C
        System.out.println("ParseBoolean converts String toboolean : " + Boolean.parseBoolean("true"));
        System.out.println("Boolean is converted into String : " + b.toString());
        System.out.println("Boolean is converted into String : " + Boolean.toString(a));
        System.out.println("Converts Boolean into Boolean : " + Boolean.valueOf(a));
        System.out.println("Converts String into Boolean : " + Boolean.valueOf("true"));    
    }
}
OUTPUT

Boolean value of a : true
Compare of b and c : 0
CompareTo of a and c : 1
Equals of b and c : true
Get Boolean from property : true
Hash code for Boolean : 1231
ParseBoolean converts String : true
Boolean is converted into String : false
Boolean is converted into String : true
Converts Boolean into Boolean : true
Converts String into Boolean : true

DESCRIPTION

In this program, all the Boolean methods used to obtain Boolean value.

THINGS TO TRY
  • At LINE A, interchange the variables a, c in compareTo method and see the output difference.
  • At LINE B, in place of "true" put "false" and see the output difference.
  • At LINE C, replace a by b and see the hash code difference.

© meritcampus 2019

All Rights Reserved.

Open In App