Default value of 'boolean' and 'Boolean' in Java
The default value for a Boolean
(object) is null
.
The default value for a boolean
(primitive) is false
.
Is Java's default value for Boolean 'true'?
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'
Default Boolean value in Java
All instance and class variables in Java are initialised with a default value:
For type
boolean
, the default value isfalse
.
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
How to set default boolean value in JPA
As far as i known there is no JPA native solution to provide default values.
Here it comes my workaround:
Non database portable solution
@Column(columnDefinition="tinyint(1) default 1")
private boolean include;
Java oriented solution
private boolean include = true;
Java oriented plus Builder pattern
@Column(nullable = false)
private Boolean include;
...
public static class Builder {
private Boolean include = true; // Here it comes your default value
public Builder include (Boolean include ) {
this.include = include ;
return this;
}
// Use the pattern builder whenever you need to persist a new entity.
public MyEntity build() {
MyEntity myEntity = new MyEntity ();
myEntity .setinclude (include );
return myEntity;
}
...
}
This is my favorite and less intrusive. Basically it delegates the task to define the default value to the Builder pattern in your entity.
Sending default boolean value into a method in Java
The are no default values for method arguments in Java. You can use method overloading instead.
private static String methodName(String name) {
return methodName(name,true);
}
private static String methodName(String name, boolean failOnMissing) {
...
}
Is a boolean instance variable default value true or false
If you create an instance variable in a class, is the default value true or false until otherwise changed?
The default value is false
. (JLS 4.12.5)
Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class?
I assume you mean, is it good practice to define your boolean instance variables so that you can rely on default initialization.
The answer is: No. It is not good practice:
It is good practice to define the instance variables so that they make sense to the reader of the code:
// Good (probably)
private boolean isValid = true;
// Bad (probably)
private boolean isNotValid; // so that I can rely on default init(Now, it may make your code easier to understand if the variable is negated ... but the point is that you should decide based on what makes the code easy to understand ... not on based exploiting default initialization.)
It is bad practice to spend time worrying about performance issues at this level of granularity. The chances are that performance benefit of avoiding an explicit initialization is insignificant.
Can't set boolean field explicitly to null
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;
Related Topics
Converting String to "Character" Array in Java
Jackson - Serialization of Entities with Birectional Relationships (Avoiding Cycles)
Time Complexity of Java's Substring()
Java: Object to Byte[] and Byte[] to Object Converter (For Tokyo Cabinet)
What Does the Registernatives() Method Do
What Is the Equivalent of Java Static Methods in Kotlin
How Does the Spring @Responsebody Annotation Work
Differencebetween Iterator and Iterable and How to Use Them
Correct Use of Flush() in JPA/Hibernate
Is This the Best Way to Rewrite the Content of a File in Java
What Hashing Function Does Java Use to Implement Hashtable Class
Spring - Redirect After Post (Even with Validation Errors)
How to Compile a Java Source File Which Is Encoded as "Utf-8"