While I know that by definition a boolean consists of only two states, true or false. I was wondering what value does a boolean have before it is initialized with one of these states.
-
48The third boolean state is usually FileNotFound . – Brian Jun 04 '09 at 18:07
-
6@Brian, doesn't that depend on whether you implement the Paula Bean? – Paul Tomblin Jun 04 '09 at 18:16
-
11@Paul: When you are not observing, it does not exist. The quantum function collapses only when you open the door :D – talonx Jun 04 '09 at 18:27
-
3@talnox When you are not observing it is probabilities cloud! – Ali Shakiba May 05 '11 at 19:33
-
[FileNotFound reference](http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx) – andyb Mar 20 '14 at 15:50
8 Answers
Edit: By popular demand:
unless you're using the wrapped Boolean, which defaults to null. – sudhir.j

- 46,244
- 33
- 149
- 164
-
3In general, Java variables default to what C programmers might call "falsey" values—e.g. 0, null, false, etc. – hbw Jun 04 '09 at 18:10
-
11unless you're using the wrapped Boolean, which defaults to null. – Sudhir Jonathan Jun 04 '09 at 18:27
-
@sudhir.j absolutely right. I hope that the answer gets edited to include that. – Ian McLaird Jun 04 '09 at 18:32
-
I did happen to know that fact sudhir.j, that is why I tagged the question as primitive, but I also hope the answer gets edited to include it for the benefit of others. – Bobby Jun 04 '09 at 21:41
If it is a local variable, it is a compiler error to reference it before it was initialized. If it is a field, it is initialized to false.

- 90,445
- 31
- 189
- 263
public class NewMain {
boolean foo;
Boolean bar;
public static void main(String[] args) {
NewMain obj = new NewMain();
obj.whatBoolean();
}
public void whatBoolean() {
System.out.println(foo);
System.out.println(bar);
}
}
outputs
false
null
I know this was more philosophical of a question, but thanks to autoboxing you can use Java as a almost truly OO language (I hate having primitive types... now only if it would work in reverse too). It does, however, change the behavior when you use an object (for the better IMO).

- 11,032
- 1
- 23
- 21
FYI, boolean defaults to false, primitive numbers default to 0, 0L, 0f or 0d as appropriate, char defaults to '\0', Object references (such as Boolean) default to null.
This also applies to the contents of arrays. (A common gotcha is that an array of Objects is initially full of null values)

- 525,659
- 79
- 751
- 1,130
If you had something like
boolean isTrue;
System.out.println(isTrue);
You should get a compile time error because the boolean wasn't initialized. By default when you try to initialize this it will be set to false;

- 3,149
- 9
- 43
- 73
-
2Yes, but if the boolean is an instance variable, it defaults to false, no warning. – Lawrence Dol Jun 04 '09 at 18:10