void : Java Glossary

go to home page V words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ 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)
void
A keyword used in Java to indicate a method does not return a value. It is not used to indicate a method requires no parameters. Constructors don’t use the void keyword even though they don’t return an object.

Void is also a dummy class.

Void also refers generically to the various flavours of hollow Strings, namely: blank (i.e x.length() != 0 && x.trim().length() == 0, e.g. "  "), empty (i.e. x.length() == 0, e.g. "") and null (i.e. x == null). One of the most common sources of error in Java programs comes from confusing the three different flavours of void strings. Java code often has a maintenance timebomb ticking in it in the form of inconsistent representation of void strings and objects. It is unwise to just let NullPointerException find your problems for you. The problem may surface many kilometers from the true source of the problem. There will always be one more bug. It is better to decide on your canonical void representation and be fanatically rigid about it.

Which void representation should you use?

If you don’t use a canonical representation and consistent checking for the various void forms you will have two classes of bug:
  1. Void Strings mistakenly treated as if they were non-void. The effect can be indirect and very hard to track down. e.g. You may test for the presence of A and if it exists do some operation on B.
  2. Non-void Strings are mistakenly treated as if they were void. Data just disappear or are ignored.
These bugs can be a bitch to track down because the variant voids or void itself are often rare for many data fields. It may require a particular improbable constellation of data for the bug to surface.

There are two plausible canonical representations for void namely null and empty (""). Normally you combine either of them with canthappen. The four ways you might represent void Strings are:

  1. canthappen

    Check for all forms of void parameters and throw an IllegalArgumentException or NullPointerException if Java won’t throw one all by itself soon. It is far easier to prevent bad data (i.e. inconsistent void representations) getting into your objects and databases that to deal with it once it gets in. You might wrap this code in if (debugging) so it can be turned off for production speed. Unfortunately Java has no design-by-contract features to do this more elegantly. You can catch these with a neverNull method.
  2. null

    Make sure all void inputs are converted to null using a possiblyNull method.
  3. empty

    Make sure all void inputs are converted to "" using a possiblyEmpty method.
  4. blank

    Not recommended.
You are just asking for trouble if you use a variety of void representations. The code may be clear to you, but will drive people who come after you maintaining the code crazy.

In a similar way you can get into trouble returning null instead of an empty Collection. One convention, used by File.list, is to return an empty array or Collection for no elements, and null to represent the result of an invalid request.

Note to C programmers

There are a few differences between C and Java that may confuse you.
Sun’s Javadoc on the Void class : available:

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.58]
You are visitor number 42,199.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/void.html J:\mindprod\jgloss\void.html