2

I have this:

  public static class RaceParam {

    boolean keep = null; // does not compile

  }

that won't compile because you can't assign null to a primitive. So that begs the question, what value does keep get when we do this:

  public static class RaceParam {

    boolean keep;

  }

does it default to false or true? Seems dangerous to default to something.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • User wrapper class if you want to set null. use Boolean instead of boolean. – Akash Shah Feb 22 '19 at 06:30
  • Yes, it's (potentially) dangerous to rely on the default value of a primitive. You should assign an explicit default value if possible. Regarding the `null` problem, you may use a `Boolean` field instead. – Tim Biegeleisen Feb 22 '19 at 06:31
  • @TimBiegeleisen sure but what is the default value of a primitive boolean? :) –  Feb 22 '19 at 06:32
  • Disagree it's "dangerous" to rely on default values. Default values in Java are well defined. They're zero for all primitives except `boolean`, which is `false`. For references it's `null`. – markspace Feb 22 '19 at 06:32
  • 1
    @MrCholo It's `false`. – Tim Biegeleisen Feb 22 '19 at 06:33
  • 1
    @markspace I agree, but from a business logic point of view, sometimes it can be safer to always explicitly assign some default value. – Tim Biegeleisen Feb 22 '19 at 06:33
  • @MichaWiedenmann The [currently linked original](https://stackoverflow.com/q/11047276/642706) of this duplicate seems to be different than the one you quoted. This Question here is indeed a duplicate of the currently linked original. – Basil Bourque Feb 22 '19 at 06:37
  • @MichaWiedenmann The *only* place I have seen the phrase “What is the default of bool?” is in your comments. I’ve no idea where you you got that from. I suspected someone somewhere changed something, hence my comment to you as a heads-up about current conditions. Both this Question and it’s linked original ask why can’t we assign null to a Boolean primitive. – Basil Bourque Feb 22 '19 at 07:12
  • @BasilBourque Ah, okay, I think I got you now. 1. Sorry, bool is a typo should have been boolean. 2. OP states as a *fact* that you cannot assign null, he seems to know that and he does not ask that. Instead OP asks: "So that begs the question, what value does keep get when we do this:" – Micha Wiedenmann Feb 22 '19 at 07:40

3 Answers3

5

boolean defaults to false

boolean is a primitive and can only hold the values true or false. The default value is false.

boolean keep;  // Defaults to `false`.

Use the wrapper class Boolean if you want to assign a null value. For eg :

Boolean keep = null;
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 1
    that doesn't answer the question tho, I am wondering what the default value is for the keep field if it's not explicitly assigned to null..and the reasoning behind it if possible. –  Feb 22 '19 at 06:31
  • 1
    Updated the answer. Default value is false. – Nicholas K Feb 22 '19 at 06:32
  • 1
    @MrCholo To be fair, you didn't ask what happens to a `boolean` field that does not get assigned anything too. – Jai Feb 22 '19 at 06:33
  • @Jai MrCholo is always right! –  Feb 22 '19 at 07:47
3

From jls 4.12.5. Initial Values of Variables:

Each class variable, instance variable, or array component is initialized with a default value when it is created

For type boolean, the default value is false.

Community
  • 1
  • 1
xingbin
  • 27,410
  • 9
  • 53
  • 103
-1

The default value of Boolean keep is null, just like any other object. You have to explicitly initialize it.

xingbin
  • 27,410
  • 9
  • 53
  • 103
Prasanth Nair
  • 489
  • 3
  • 12