In Java, the boolean keyword is used to declare the variable as a boolean type that can have only two values either true or false.
Default value of boolean in Java
💡 Did You Know?
The default value of primitive boolean is false whereas the Boolean object is null.
The default value of primitive boolean is false whereas the Boolean object is null.
Java program to find the default value of boolean
public class DefaultBooleanValue{
boolean primitiveBoolean;
Boolean objectBoolean;
public static void main(String args[]) {
DefaultBooleanValue obj = new DefaultBooleanValue();
obj.printDefaultBooleanValues();
}
public void printDefaultBooleanValues() {
System.out.println("default value of primitive boolean is: "+ primitiveBoolean);
System.out.println("default value of Boolean object is: "+ objectBoolean);
}
}
Output:
default value of primitive boolean is: false
default value of Boolean object is: null
From the above example, you can find that the default value of boolean is false and the Boolean object is null.
Default value of boolean array in Java
The boolean array can contain only boolean datatype values and the default value is false. Similarly, the boolean array of reference types has the default value of null.
💡 Did You Know?
The default value of the primitive boolean array is false whereas the Boolean object array is null.
The default value of the primitive boolean array is false whereas the Boolean object array is null.
Java program to find the default value of a boolean array
public class DefaultBooleanArrayValue {
boolean[] primitiveBooleanArray = new boolean[3];
Boolean[] objectBooleanArray = new Boolean[3];
int length = primitiveBooleanArray.length;
public static void main(String args[]) {
DefaultBooleanArrayValue obj = new DefaultBooleanArrayValue();
obj.printDefaultBooleanValues();
}
public void printDefaultBooleanValues() {
for(int i=0 ; i < length; i++) {
System.out.println("default value of primitive boolean array is: "+ primitiveBooleanArray[i]);
System.out.println("default value of Boolean object array is: "+ objectBooleanArray[i]);
}
}
}
Output:
default value of primitive boolean array is: false
default value of Boolean object array is: null
default value of primitive boolean array is: false
default value of Boolean object array is: null
default value of primitive boolean array is: false
default value of Boolean object array is: null
That's all for today, please mention in the comments in case you have any questions related to default value of boolean and boolean array in Java.