Jump to content

Enumeration (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bluemoose (talk | contribs) at 21:55, 5 January 2006 (AWB Assisted clean up). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Enumeration is a special technique used in programming languages. In general it associates each element of an ordered list with a corresponding integer number. This allows one to work with descriptive values instead of using magic numbers.

Java

In the Java programming language a program line looks like this:

enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

It is important to note, that a Variable defined in a simple way like

Days day;

is now typesafe. This means, you can only assign enum values to such a variable, not integer numbers.

There is also the possibility to assign each enum Entry additional values and certain functions for enhancement. This is a huge step ahead of C++, where enum Variables are only glorified integers. Java has certain tools for operating with enums and they can be seen more like a full-featured class than a description for integers.