Sobes.tech
Junior — Middle

What value is a boolean variable initialized with if no explicit value is assigned?

sobes.tech AI

Answer from AI

In Java, a boolean variable that is a class field (declared at the class level, not inside a method) is initialized with the default value — false. However, if a boolean is declared as a local variable inside a method, it is not automatically initialized, and attempting to use it without explicit assignment will result in a compile-time error.

Example:

public class Example {
    boolean flag; // defaults to false

    public void method() {
        boolean localFlag; // not initialized
        // System.out.println(localFlag); // compile error
        localFlag = true;
        System.out.println(localFlag); // true
    }
}
What value is a boolean variable initialized with if… - sobes.tech