All instance and class variables in Java are initialised with a default value:
For type boolean
, the default value is false
.
So your two statements are functionally equivalent in a single-threaded application.
Note however that boolean b = false;
will lead to two write operations: b
will first be assigned its default value false
then it will be assigned its initial value (which happens to be false
as well). This may have an importance in a multi-threaded context. See this example of how explicitly setting the default value can introduce a data race.
Relying on such default values, however, is generally considered bad programming style.
I would argue the opposite: explicitly setting default values is bad practice:
- it introduces unnecessary clutter
- it may introduce subtle concurrency issues