Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use Boolean instead of boolean? [duplicate]

Tags:

java

When should I use Boolean instead of boolean?. I mean, why would I want to have a null value in a variable which should contain either "true" or "false".. One spontaneous answer (of most people) would be if the value is unknown. i.e, if we don't know whether the value is true or false. But from a programming perspective, I think using Boolean may break the code as we will not know whats inside. So, I think using the primitive type is better than the wrapper.. Correct me if I am wrong.

like image 592
TheLostMind Avatar asked Sep 23 '13 06:09

TheLostMind


People also ask

Which is better to use boolean or boolean?

It is recommended that you use the Boolean() function to convert a value of a different type to a Boolean type, but you should never use the Boolean as a wrapper object of a primitive boolean value.

What is diff between boolean and 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.

Is it boolean or boolean?

In computer science, a boolean or bool is a data type with two possible values: true or false. It is named after the English mathematician and logician George Boole, whose algebraic and logical systems are used in all modern digital computers. Boolean is pronounced BOOL-ee-an.

What is difference between boolean and boolean in typescript?

Uppercase Boolean is an object type. Lowercase boolean is a primitive type. You should always use boolean (the primitive type in your programs). This is because, the Typescript type system does not force an object to its primitive type, while JavaScript does.


1 Answers

Generally speaking, the wrapper classes are used in cases where an object is required or strongly preferred. Outside of these situations, it's better to use the primitive types, since they have lower overhead, you can use ==, etc. There are two and a half major situations where this is frequently seen:

  • Collections. This is now a subset of the next case, but even before Java 5 the Collections classes only supported objects as keys and values, and this hasn't changed.
  • Generics. Generic types can only work with objects, not primitives, and so if you're using "boolean" as a type parameter, it has to be the wrapper class. For example, if you're using a Future, you have to use a Boolean instead of a boolean. (HT @user949300)
  • ORM. JPA and other ORM systems technically can use primitive fields, but it's customary to use the wrapper classes, since the overhead is high enough that that doesn't really matter anyway, and the wrapper classes can represent a NULL value that might be present in the database. It's usually still better to forbid nulls and use a primitive for booleans, though, since semantically a default is usually better than "undefined".

Since boolean values are restricted to either true or false, it's uncommon to see them used in Collections or Generics; generally speaking, if you'd have a boolean as a value, you'll just use Collection#contains instead.

like image 161
chrylis -cautiouslyoptimistic- Avatar answered Sep 22 '22 11:09

chrylis -cautiouslyoptimistic-