Initialise Boolean Variable in Java

In Java, Boolean is a wrapper class that wraps the primitive datatype boolean type. This wrapper is added in the documentation in Java version 8. This type stores either two of the values, true or false. Additionally, the wrapper provides static and final variables like TRUE and FALSE values.

Below is the code block demonstrating the Boolean wrapper class.

public class Main {
    public static void main(String[] args) {
        Boolean isStatus = true;
        if (isStatus) {
            System.out.println("The status is true");
        } else {
            System.out.println("The status is false");
        }
    }
}

The code above instantiates the isStatus Boolean variable to true value. When the value is used inside the if condition, the results can be a Boolean true or false value. Based on the result, a print statement will be printed.

The Boolean can get initialized with boolean values, some simple conditions, or expressions that result in boolean. When a primitive data type is used instead of the wrapper class, there is no compulsion to initialize the variable. The default value of the primitive is a false value.

The resultant output of the program above is shown below. As the variable gets initialized to true, the statement inside the true block is executed.

Output:

The status is true
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Boolean

  • Boolean to String in Java