How to Initialise Boolean Variable in Java

Rashmi Patidar Feb 02, 2024
How to 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
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Boolean