Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java's default value for Boolean 'true'? [duplicate]

Tags:

java

boolean

Why does private Boolean shouldDropTables; assign true by default to the variable instead of NULL, like when writing private Integer anInteger;?

I am asking because I came across some code where there was an evaluation on a shouldDropTables Boolean variable being NULL or not determining whether to execute a method.

like image 295
camiloqp Avatar asked Sep 11 '25 01:09

camiloqp


1 Answers

Boolean (with a uppercase 'B') is a Boolean object, which if not assigned a value, will default to null. boolean (with a lowercase 'b') is a boolean primitive, which if not assigned a value, will default to false.

Boolean objectBoolean;
boolean primitiveBoolean;

System.out.println(objectBoolean); // will print 'null'
System.out.println(primitiveBoolean); // will print 'false'
like image 165
nicholas.hauschild Avatar answered Sep 15 '25 17:09

nicholas.hauschild