Default Boolean value in Java
boolean, java
Solution
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
Problem
I just want to know if there is a difference in Java between: ``` private boolean someValue; private boolean someValue = false; ``` The second line maybe is just a time wasting? EDIT (SUMMARY): From the answers I found that there is almost no difference, but: "Relying on such default values, however, is generally considered bad programming style." But there are some strong arguments not to do so - see accepted answer below. EDIT 2 I found that in some cases `boolean` value must be initialized, otherwise the code will not compile: ``` boolean someValue; if (someValue) { // Error here // Do something } ``` In my NetBeans IDE I got the error - "variable someValue might not have been initialized". It's getting interesting.. :)