boolean : Java Glossary


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


Avoid Don’t


This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
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.

Your face IP:[]
You are visitor number