Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean vs boolean in Java

Tags:

java

boolean

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?

like image 931
Neel Avatar asked Sep 16 '10 16:09

Neel


People also ask

What is the difference between boolean and boolean Java?

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 .

Is it boolean or boolean in Java?

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.

Is boolean and boolean same in Java?

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, 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.


1 Answers

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.

like image 81
jmj Avatar answered Oct 10 '22 05:10

jmj