Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > difference between 'boolean' and 'java.lang.Boolean'

Reply
Thread Tools

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

 
 
J Leonard
Guest
Posts: n/a
 
      01-18-2008
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

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      01-18-2008
J Leonard <(E-Mail Removed)> writes:
>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«).

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-18-2008
On Fri, 18 Jan 2008 13:25:05 -0800 (PST), J Leonard
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

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


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
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-19-2008
J Leonard said :
>> What is the difference between 'boolean' the built-in type and
>> 'java.lang.Boolean' the class?


Roedy Green wrote:
> 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.

--
Lew
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      01-19-2008
J Leonard wrote:
> 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 04-15-2011 04:00 AM
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
difference between between these "char"s arnuld C++ 33 03-05-2007 03:11 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments