boolean : Java Glossary
home B words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
boolean
Java has a logical type that can only have the values true or false called boolean. The boolean type deals with only a single bit at a time. An immutable object wrapper around a boolean is called a Boolean. You can do boolean operations on ints or longs, many bits at a time using &, |, ^ and ~. With boolean, you can only process one bit at a time, typically with !, && and ||. For bit strings longer than 64 bits, use java. util. BitSet. Even experienced programmers sometimes lapse into redundant newbie-like baby talk when using booleans. To avoid that embarrassement see newbie.

boolean expressions

Anything you can put inside an if (...) is a boolean expression e.g.
// example boolean expressions
x > 2
0 <= y && y <= 10
! (0 <= y && y <= 10 )
button.isVisible()
A boolean expression evaluates to true or false. You can store it in a boolean variable e.g.
// storing a boolean expression in a variable
boolean whether = x > 2;
You can print out a boolean variable. You don’t need an if to do it.
// displaying a boolean variable on the console
System.out.println( whether );
You can use a boolean variable in an if later rather than recalculating the expression.
// referencing a boolean variable in an if
if ( whether ) makeDate();
if ( ! whether && done() ) finish();

boolean vs int

In C, booleans and ints are pretty well interchangeable. In Java they are totally separate types. You can’t do the following in Java:
// ILLEGAL IN JAVA!!  int is not the same as boolean
int seen = button.isVisible();
if ( seen ) doSomething();
You must write:
// boolean must be used in Java where in C you could use an int.
boolean visible = button.isVisible();
if ( visible ) doSomething();

// or using an int with int-style syntax.
int seen = button.isVisible() ? 0 : 1;
if ( seen != 0 ) doSomething();

& vs && and | vs ||

&& and || are sometimes called short circuit & and | or McCarthy & and |.

Stuttering

Avoid goony expressions like:
// Goony stuttering code often written by newbies

if ( whether == true )

// or

if ( whether == false )

// or

if ( whether == true && done == false )
Don’t stutter, just write:
// terse code

if ( whether )

// or

if ( !whether )

// or

if ( whether && !done )

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 25,831. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/boolean.html J:\mindprod\jgloss\boolean.html