There are discussions around Integer
vs int
in Java. The default value of the former is null
while in the latter it's 0
. How about Boolean
vs boolean
?
A variable in my application can have 0
/1
values. I would like to use boolean
/Boolean
and prefer not to use int
. Can I use Boolean
/boolean
instead?
In Java, a boolean is a literal true or false , while Boolean is an object wrapper for a boolean . There is seldom a reason to use a Boolean over a boolean except in cases when an object reference is required, such as in a List .
Java provides a wrapper class Boolean in java. lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean.
boolean is a primitive and Boolean is as object wrapper. So boolean, is the type whose values are either true or false while the other is an object. If you wanted to convert a string to a boolean you could try Boolean.
In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.
Yes you can use Boolean
/boolean
instead.
First one is Object and second one is primitive type.
On first one, you will get more methods which will be useful.
Second one is cheap considering memory expenseThe second will save you a lot more memory, so go for it
Now choose your way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With