Java boolean
A boolean in java is a primitive data type with only two possible values: true or false. This article will explain what is a boolean, how to use boolean in java, its default value and boolean array with example programs.

Boolean values are most commonly used in
1. if statements where the condition should result in true or false.
2. Java ternary operator where the first operand or the expression should return a boolean.
3. Comparison of values with each other using logical(&& or ||) or equality(==) operators always results in a boolean value.
A variable of type boolean is declared with boolean keyword.

boolean example
Below is an example program which declares and prints the values of boolean variables.

public class BooleanDeclaration {

  public static void main(String[] a) {
    // declare variables
    boolean start = true;
    boolean stop = false;
    // print variables
    System.out.println("Start value is: " + start);
    System.out.println("Stop value is: " + stop);
  }
}

This prints

Start value is: true
Stop value is: false

Boolean expression
An expression which returns a boolean value is a boolean expression in java. As stated earlier, comparisons performed with logical or equality operator always return a boolean value.
Below is an example program which shows the usage of a boolean expression to compare two integers with if statement

public class BooleanDeclaration { 
  public static void main(String[] a) { 
    // declare variables 
    int numOne = 10; 
    int numTwo = 20; 
    // compare variables 
    if(numOne == numTwo) {
      System.out.println("Values are equal"); 
    } else {
      System.out.println("Values are not equal"); 
    } 
  }
}

Output is

Values are equal

Java boolean method
A method in java may also return a boolean value. Return type of such method must be boolean. Example program is

public class BooleanDeclaration { 
  public static void main(String[] a) { 
    // declare variables 
    int numOne = 10; 
    int numTwo = 20; 
    if(compareNumbers(numOne, numTwo)){
      System.out.println("Values are equal"); 
    } else { 
      System.out.println("Values are not equal"); 
    } 
  } 

  // boolean method
  static boolean compareNumbers(int numOne, int numTwo) {
    // compare variables 
    if(numOne == numTwo) {
      return true;
    } else {
      return false;
    }
  }
}

A boolean method should return either true or false value.
As already stated, comparison expression results in a boolean value. Thus, the method in above example can be shortened by directly returning result of boolean expression as shown below.

static boolean compareNumbers(int numOne, int numTwo) { 
  // compare variables 
  return numOne == numTwo;
}

Boolean wrapper
A wrapper class is an object which wraps the primitive type. There are places where you can only use an object and not a primitive value such as in Collections, while creating an array list of type boolean, you need an object.

Wrapper classes have the same name as the primitive type with the first letter capital. So, wrapper class for boolean data type is Boolean and it wraps or represents the specified boolean value.

Boolean wrapper class contains utility methods to convert a boolean to string and string to boolean.

  • To create a wrapper object from primitive value, use its valueOf() method which accepts a primitive value as argument. valueOf() is a static method, so you don’t need an object to invoke it.
  • To get primitive value from a wrapper object, use its booleanValue() method.
  • To convert a boolean primitive to String, call toString() method on wrapper object.
  • To compare two boolean wrappers, use its equals() method. It compares the value of the underlying primitives and returns true if they are same.

Example,

public class BooleanToString {

  public static void main(String[] args) {
    // declare a boolean primitive
    boolean primitive = true;
    // convert to wrapper object
    Boolean wrapper = Boolean.valueOf(primitive);
    // get its boolean value
    System.out.println(wrapper.booleanValue());
    // convert boolean to string
    String boolString = wrapper.toString();
    System.out.println("Boolean to string is: " + boolString);
    // create another wrapper
    Boolean wrapperTwo = Boolean.valueOf(false);
    // compare wrapper objects
    if (wrapper.equals(wrapperTwo)) {
      System.out.println("Primitive values are same");
    } else {
      System.out.println("Primitive values are not same");
    }
  }
}

Output is

true
Boolean to string is: true
Primitive values are not same

Null handling
You can not initialize a boolean primitive to null, but a boolean wrapper can be initialized to null, which means that you can compare it to null as shown below.

Boolean b = null;
if(b == null) {
  System.out.println("Nothing here");
}

Further, if a null is supplied to valueOf() method while creating a wrapper object, then the underlying primitive value is false. Example,

Boolean wrapper = Boolean.valueOf(null);
if(wrapper.booleanValue()) {
  System.out.println("boolean is true");
} else {
  System.out.println("boolean is false");
}

Boolean arrays
An array in java is a collection of values of same data type. You can create an array of boolean values. Syntax is

boolean[] var = new boolean[size];

Providing a size is mandatory.
Elements of the array are indexed starting from 0, which means that first element is at index 0, second is at index 1 and so on.
Default values of elements of a boolean array are false.

There is another way of initializing a boolean array where you can provide initial values as shown below

boolean[] arr = new boolean {true, false, true, false, false};

This method is suitable when the size of array is small. Example program is given below

public class DatamigrationApplication {
  public static void main(String[] args) throws IOException {
    boolean[] arr = new boolean[5];
    for (boolean bool : arr) {
      System.out.println("Default value is: "+bool);
    }
  }
}

This prints

Default value is: false
Default value is: false
Default value is: false
Default value is: false
Default value is: false

All the elements are initialized to false by default. If the array is of Boolean wrapper objects, then the elements are initialized to null by default.
This is because the elements of wrapper array are objects and objects in java are initialized to null by default.

We can assign value to an array element using its index and assignment operator as shown below.
// assign element at third place

arr[2] = true;

That is all on boolean values in java. Hope the article was helpful.