boolean : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (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;
// displaying a boolean variable on the console
out.println( whether );
You can use a boolean variable in an if later rather than
// 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.
// ILLEGAL IN JAVA!!  int is not the same as boolean
int seen = button.isVisible();
if ( seen ) doSomething();
// 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 Don’t

Gotchas


This page is posted
on the web at:

http://mindprod.com/jgloss/boolean.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\boolean.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
Blog
IP:[65.110.21.43]
Your face IP:[54.82.27.141]
You are visitor number