Default value of 'boolean' and 'Boolean' in Java

477,019

Solution 1

The default value for a Boolean (object) is null.
The default value for a boolean (primitive) is false.

Solution 2

The default value of any Object, such as Boolean, is null.

The default value for a boolean is false.

Note: Every primitive has a wrapper class. Every wrapper uses a reference which has a default of null. Primitives have different default values:

boolean -> false

byte, char, short, int, long -> 0

float, double -> 0.0

Note (2): void has a wrapper Void which also has a default of null and is it's only possible value (without using hacks).

Solution 3

boolean
Can be true or false.
Default value is false.

(Source: Java Primitive Variables)

Boolean
Can be a Boolean object representing true or false, or can be null.
Default value is null.

Solution 4

If you need to ask, then you need to explicitly initialize your fields/variables, because if you have to look it up, then chances are someone else needs to do that too.

The value for a primitive boolean is false as can be seen here.

As mentioned by others the value for a Boolean will be null by default.

Solution 5

Boolean is an Object. So if it's an instance variable it will be null. If it's declared within a method you will have to initialize it, or there will be a compiler error.

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 declared within a method you will still have to initialize it to either true or false, or there will be a compiler error.

Share:
477,019

Related videos on Youtube

Makky
Author by

Makky

Professional Software Engineer with couple of years experience in core Java and web development. Currently I am spending time on ExtJS , Mule and MyBatis. In my spare time , I play games, specially Street Fighter. I used to be an online warrior, not any more. I aint get much time.

Updated on October 13, 2020

Comments

  • Makky
    Makky over 3 years

    What are the default values of boolean (primitive) and Boolean (primitive wrapper) in Java?

  • LoBo
    LoBo over 8 years
    Thank you for reference to official source. As to initialization, there is no need to compensate for a lack of knowledge.
  • Peter Tillemans
    Peter Tillemans over 8 years
    'Lack of knowledge' implies that there is some expected level to compare to. If this minimal knowledge level is clear, I agree. If not, I find it prudent to err on being more explicit. In all this is a rather personal/team decision/opinion.
  • ferekdoley
    ferekdoley over 8 years
    @LoBo - I think there is nothing wrong with compensating for a potential lack of knowledge. You don't know who will be maintaining your code down the line. Anything that makes your code more easily understood and/or readable, while having little impact on performance, can only be a good thing.
  • Suma
    Suma almost 6 years
    I think one could mention Boolean is Object, that would make the first line meaning obvious. This sounds pretty basic, but for someone asking a basic question like this it may help.
  • Radiodef
    Radiodef almost 6 years
    I think those 3 downvotes are because you say "there is no default" which is a fairly unclear statement. As you say, an uninitialized object reference points to null. null is the default, so I'm not sure what you are referring to when you say there isn't one. (Did you maybe mean something like "Boolean is neither true nor false by default"?)
  • Stealth Rabbi
    Stealth Rabbi almost 6 years
    Yes, what you put is more accurate. There is already an accepted answer that is more concise. I'm not sure what is gained by both downvoting a post and editing it.
  • Radiodef
    Radiodef almost 6 years
    To clarify, I didn't downvote and my edit was completely unrelated to my comment. I'd recommend editing the answer if you approve of that suggested phrasing, and then we can delete our comments to clean up.
  • very
    very over 5 years
    please add official source link
  • Freddie
    Freddie almost 4 years
    I agree with @Lobo on this one. You should assume your future code reader knows how to write Java code. It is a bad idea to explain how Java works, in the code. That's what stackoverflow is for.
  • improbable
    improbable about 3 years
    This answer contains misleading/partially false information and it should be downvoted/removed in its current state.
  • Stealth Rabbi
    Stealth Rabbi about 3 years
    @improbable what is misleading / partially false? Can you clarify?
  • improbable
    improbable about 3 years
    > There is no default for Boolean @StealthRabbi