8

I have read several previously asked questions and answers on this topic [or quite similar], but none of them have really addressed this point-blank. When declaring a new boolean varibale, is it redundant [e.g. unnecessary] to initialize it to false?

boolean selectedZone = false;

versus just declaring

boolean selectedZone;

The other posts I have looked at are Why is Java's default value for Boolean set to true? and Default value of Boolean in java

Community
  • 1
  • 1
triforceofcourage
  • 4,928
  • 2
  • 16
  • 21
  • So the question is, are booleans initialized to "false" by default. I actually don't know and am curious, after coming from `C` I take nothing for granted, so I'm curious :) – EpicPandaForce Jul 23 '14 at 14:52
  • Well I believe I would be correct in saying that booleans *are* false by default, but is there any reason that just declaring it and leaving it as such is less right then to initialize it to false at time of declaration. – triforceofcourage Jul 23 '14 at 14:53
  • I would go for the first option, just for the sake of making sure, and also it makes the code more clear, as explicitly setting the variable to **false** may improve better understanding of the algorithm – Matias Cicero Jul 23 '14 at 14:53
  • http://stackoverflow.com/questions/21509150/default-boolean-value-in-java – assylias Jul 23 '14 at 14:56

4 Answers4

7

It is redundant in most cases. The exception is if it's a local variable, in which case it needs to be initialized before being used. Examples:

class Example{
    boolean value1;//valid, initialized to false
    public void doStuff(){
        boolean value2;//valid, but uninitialized
        System.out.println(value1);//prints "false" (assuming value1 hasn't been changed)
        System.out.println(value2);//invalid, because value2 isn't initialized. This line won't compile.

Some documentation on this: https://web.archive.org/web/20140814175549/https://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#96595

Even though variables are initialized, you still might want to explicitly declare their initial values. It can make the code more legible, and lets people reading your code know that it's a conscious decision to set it to that value.

Related

resueman
  • 10,572
  • 10
  • 31
  • 45
5

It is redundant, but I find it clearer.

The same goes for :

int var = 0;

vs.

int var;

I didn't make this up :

Default Values

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false

(Source)

However, as the comments suggest, if it's a local variable, it must be initialized.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • are you 100% certain that is initialized to zero? Well, I'll just go and check... – EpicPandaForce Jul 23 '14 at 14:52
  • @Zhuinden, yes int's in Java are initialized to 0 by default. – Martin Konecny Jul 23 '14 at 14:54
  • 1
    `int var;` followed by `System.out.println("" + var);` gives "var might not have been initialized" compilation error as local variable, but not as instance or class (static) variable in which case they are initialized to `0` – EpicPandaForce Jul 23 '14 at 14:55
  • 1
    yes that's exactly what I said, if you have it as a field or a static field then it is initialized to `0` but otherwise it throws compilation error. – EpicPandaForce Jul 23 '14 at 14:58
  • @Zhuinden You don't mention if your values are *fields* or *local variables* in your question. Whether the code compiles depends on that! – Kevin L Jul 23 '14 at 14:59
  • @KevinL after I tested it in the IDE I figured that out too :P – EpicPandaForce Jul 23 '14 at 16:33
2

Yes, you can skip explicitly initializing it to false - however just because you can doesn't mean you should.

Explicitly setting to false makes code much clearer (and easier for developers coming from other languages where this behaviour is not default).

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • 1
    Point for you. The reason for doing it is very simple: it takes away any doubt. If I see an uninitialized variable I can only wonder "Was that intentionally left uninitialized because it should be false, or did I perhaps find a bug?" Especially in the average application which is not so small, there is ample opportunity for bugs to keep existing unnoticed and so one should make all effort to take away any doubts. – Gimby Jul 23 '14 at 15:51
1

Fields and array components are initialized to zero, local variables are not. See JLS: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5

Kevin L
  • 1,066
  • 3
  • 12
  • 21