Course
Boolean class
Java Tutorial
This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.
Boolean Class
Java Boolean Class
The Java Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.
Boolean Class Declaration in Java
Following is the declaration for java.lang.Boolean class
public final class Boolean extends Object implements Serializable, Comparable<Boolean>
Boolean Class Fields
Following are the fields for java.lang.Boolean class
- static Boolean FALSE − This is the Boolean object corresponding to the primitive value false.
- static Boolean TRUE − This is the Boolean object corresponding to the primitive value true.
- static Class<Boolean> TYPE − This is the Class object representing the primitive type boolean.
Boolean Class Constructors
Boolean Class Methods
Methods Inherited
This class inherits methods from the following classes
- java.lang.Object
Example of Java Boolean Class
The following example shows the usage of some important methods provided by Boolean class.
package com.tutorialspoint;public class BooleanDemo { public static void main(String[] args) {
// create 2 Boolean objects b1, b2 Boolean b1, b2;
// assign values to b1, b2 b1 = Boolean.valueOf(true); b2 = Boolean.valueOf(false);
// create an int res int res;
// compare b1 with b2 res = b1.compareTo(b2);
String str1 = "Both values are equal "; String str2 = "Object value is true"; String str3 = "Argument value is true"; if( res == 0 ) { System.out.println( str1 ); } else if( res > 0 ) { System.out.println( str2 ); } else if( res < 0 ) { System.out.println( str3 ); } }}
Output
Let us compile and run the above program, this will produce the following result
Object value is true