difference between 'boolean' and 'java.lang.Boolean'

J

J Leonard

What is the difference between 'boolean' the built-in type and
'java.lang.Boolean' the class?

When would you use one and not the other? I have a multi-dimensional
array representing the possibilities inside of cells in a Sudoku
puzzle and I am representing each of them as boolean values.

At the moment I'm using the built-in boolean. I'd like to know the
difference between these 2 forms.

J Leonard
 
S

Stefan Ram

J Leonard said:
What is the difference between 'boolean' the built-in type and
'java.lang.Boolean' the class?

The difference between 20 and 12 is 8, because a difference
operation has been defined on integers.

For types in Java, there is no difference operation specified.
Thus, the difference is »undefined«.
When would you use one and not the other?

The reasons are very similar to the reasons for »int«
and »java.lang.Integer« or so. See:

http://google.to/search?q=java+wrapper-classes

In purely object oriented programming, one would only
use »java.lang.Boolean«. But »boolean« is used instead,
because

- Java or an API might force this upon one,

- either because of an interface or a
part of the language requires it, or

- because it is

- being executed faster or

- uses less resources (memory) or

- it might be more convenient to write.
At the moment I'm using the built-in boolean.
I'd like to know the difference between these 2 forms.

These are two »types« (not »forms«).
 
L

Lew

J Leonard said :
Roedy said:
Same and the difference between int and Integer. Boolean is a boolean
primitive wrapped up in an Object.

boolean is faster and smaller, but sometimes you need an immutable
Object, then you use Boolean.

See http://mindprod.com/jgloss/intvsinteger.html

Also, object references can be null, primitives cannot be. Object types can
be type parameters in generics, making them suitable for collections and such.

Nullability is useful in JDBC contexts, and other contexts where you need an
out-of-band value like "UNKNOWN".

Some folks argue that Java should never have had primitives. Too bad for them
it does, I say.

Sidebar: java.lang.Boolean is just as "built-in" as the primitive types, as
are all things in packages that begin with "java." or "javax.". Furthermore,
it has direct compiler support, for things like autoboxing. The distinction
for which one is groping is that between "primitive" and "reference" types.
 
M

Mark Space

J said:
What is the difference between 'boolean' the built-in type and
'java.lang.Boolean' the class?

When would you use one and not the other? I have a multi-dimensional
array representing the possibilities inside of cells in a Sudoku
puzzle and I am representing each of them as boolean values.

At the moment I'm using the built-in boolean. I'd like to know the
difference between these 2 forms.

As others have mentioned, Booleans are full Objects in java, and have
methods including .clone(), .toString(), etc. Booleans also take more
memory than booleans, the latter can be as small as one bit.

However, for your problem (an array of Sudoku values) look into EnumMap
and EnumSet. These might actually be faster and better for you than
either boolean or Boolean. Enum sets and maps should be as fast as
packing bits into an integer and testing bits with bit-wise operators.
I.e., faster than an array of booleans.

<http://java.sun.com/javase/6/docs/api/java/util/EnumSet.html>

<http://java.sun.com/javase/6/docs/api/java/util/EnumMap.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top