7

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.. :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • 1
    Actually, there is a tiny difference, but the end result is the same. You should prefer the first one, generally. – JB Nizet Feb 02 '14 at 09:52
  • There is no difference; but if you use the second, then if later another programmer reviews your code and doesn't know about default values (it happens), then you will save him/her time if you explicitly specify `false` – morgano Feb 02 '14 at 09:53
  • 1
    @morgano: if another developer doesn't know about default values, then he will finally learn and know about them. It's a must-know. – JB Nizet Feb 02 '14 at 09:56
  • 1
    @JBNizet In an ideal world, every one should be a professional and know what they are doing; unfortunately we live in the Earth and that doesn't happen, embrace it! – morgano Feb 02 '14 at 10:02
  • 2
    There is no problem in not knowing everything. There is a problem in refusing to learn. What's wrong in learning that Java fields have a well-defined default value? By your reasoning, you should also add a comment "this assigns false to the variable someValue", just in case the developer doesn't really understand what assignments are. I would agree with you if the code was relying on obscure rules such as `a += j++ - --b`, but default values are not at this level. – JB Nizet Feb 02 '14 at 10:05
  • I think it's simple question - but look at the answers - I think lot's of people find it worthy. – Ernestas Gruodis Feb 02 '14 at 10:08
  • 1
    This is a good question, and it deserves to be reopened. In Java, `boolean` and `Boolean` are very different beasts; the people who flagged **duplicate** are probably unaware of this. Unfortunately, they voted on a subject they did not study enough. – Alex Cohn Dec 19 '16 at 16:52

5 Answers5

20

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
Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Yes, I agree, though official documents tells opposite... – Ernestas Gruodis Feb 02 '14 at 10:04
  • It is good practise to set a value always, because of readability. Consider also the following: If you use Boolean instead of boolean, the default value is null. – Hannes Feb 02 '14 at 10:11
  • 1
    @Hannes In the end, there is no definite answer: both are valid statements so which to use is a matter of taste and opinion... And We are talking about setting default value - if you want a Boolean to be false, then of course you should set it to false. – assylias Feb 02 '14 at 10:14
  • @assylias Totally right. But working with juniors you quickly forget about taste. You just want to minimize bugs. "Don't make me think" – Hannes Feb 02 '14 at 10:19
  • I agree -- I detest code clutter. If you write code that duplicates what the compiler already did, I start to doubt you know your language. On the other hand, if you do rely on the compiler, then you better be right. If you __don't__ set a default when it is actually needed (local variables), then I know we have a problem. – david.pfx Feb 02 '14 at 11:00
  • @trillions please add your feedback to this answer as a comment, not an edit. We don't want to be putting words into the original answerer's mouth. – JAL Feb 22 '16 at 03:11
  • @JAL, you right. thanks :) – trillions Feb 22 '16 at 06:39
  • @assylias, sorry that i was trying to modify your answer :). I was thinking it would be nice to point out that we don't need to initialize array too. such as boolean[] records = new boolean[5]; this will make sure all the booleans default as false itself. – trillions Feb 22 '16 at 06:39
  • @trillions I understand your point but it would not apply in this context - the question is asking about the difference between `int[] a;` and `int[] a = null` (the default value for arrays is null). – assylias Feb 22 '16 at 12:39
3

If you didn't initialize it, it will be false. So there is no difference between them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lakshman
  • 2,641
  • 6
  • 37
  • 63
2

The default value of boolean data type is false so we can say that there is no difference.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Kick
  • 4,823
  • 3
  • 22
  • 29
1

There is no difference, from:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Hmm.. interesting note: *Relying on such default values, however, is generally considered bad programming style.* – Ernestas Gruodis Feb 02 '14 at 09:57
  • I think this means you should initialize your variables at some stage, before they will be used. This mostly is being done in constructors, java compiler knows nothing about someValue purpose but its doing you a favor of setting it to known predictable value false. – marcinj Feb 02 '14 at 10:05
0

If you declare as a primitive i.e. boolean. The value will be false by default if it's an instance variable (or class variable).

If it's an instance variable Boolean it will be null.

If it's declared within a method you will have to initialize it, or there will be a compiler error.

StarsSky
  • 6,721
  • 6
  • 38
  • 63