Sunday, August 17, 2008

Comparison of Enum Types - Java vs C# vs C++

Enumerated Lists in Java

Each item in the list is called an enum. Each enum is of the type under which it is declared. It is no way a string, or any integer. The simplest declaration of an enumerated list in java is as follows:

enum BookType { HARDCOPY , EBOOK }; //where the semicolon is optional

Note here that HARDCOPY and EBOOK are not strings, there type is BookType. Anyways, once declared, we can use an instance of BookType as,

BookType bt1 = BookType.HARDCOPY;
BookType bt2 = BookType.EBOOK;


Beyond this, any other assignment to BookType would result in a compilation error.
Java’s coding conventions want you to capitalize all the constants in an enum type declaration. Having any other case combination is legal.

An enum in Java is just like a separate class. So, they can be declared independent of any class, like a different class. However, the only access modifier applicable to an independent enum declaration is public (and default i.e. no modifier). It can never be private or protected (obviously!).

They can also be declared as a class member; however, they can never be declared as a local type i.e. within a method! Any access modifier can be used with an enum type which is declared inside a class - It can be public, private, default (no modifier), or protected.

If an enum type is declared in classOne and it is being used in classTwo, then the assignment of values to this enum type’s instance is changed as follows:

public classOne {
enum BookType { HARDCOPY , EBOOK }
BookType bt;
}

public classTwo {
public classTwo(){
classOne instanceOne = new classOne();
instanceOne.bt = classOne.BookType.EBOOK;
// instanceOne.bt = BookType.EBOOK;
// this is wrong, compilation error.
}
}


As I said earlier, an enum type declaration is simply a kind of a class declaration and hence it can incorporate constructors, and member variables and methods. Why do you need all this? Because sometimes you really need to know more about a particular enum constant then just a weird looking capitalize word.

Consider following:

enum BookType{

HARDCOPY("expensive"), EBOOK("cheap");

public String bookPriceIdea; //bad practice: use getter/setter.

private BookType(String priceIdea) {
this.bookPriceIdea = priceIdea;
}
}


In the above example, each enum type is associated with a string that tells more about the price of the type of book. Think of anything else, and you can associate it to any enum type in a similar way. Notice that I have declared a member variable within the enum type declaration, this is allowed. Also note that the access modifier of the constructor is private, which is again strange; however, it cannot be public or protected because apparently it’s for internal use (invoked automatically), and not for any external invocation.

Optionally, you can just ignore the access modifier here to give it a default behavior. Accessing this additional attribute associated with an enum constant is easy and is as follows:


BookType bt = BookType.HARDCOPY;
String btPriceIdea = bt.bookPriceIdea;


Finally, there is something called “constant specific class body”. Within the declaration of the enum type, such a class body (that looks like an inner class) can be associated with any enum constant. Within this code section/body, you override a member function of the enum type declaration. The following example will hopefully clarify this:

enum BookType {

AMAZONKINDLEBOOK,
EBOOK,
HARDCOPY{
public boolean getBookDependencyOnElectronics(){
return false;
}
};

public boolean getBookDependencyOnElectronics(){
return true;
}
}


-------------------------------------------------------------------------------------
Enumerated Lists in C#

Simple declaration:

Enum enum-type-name { enumConstant1, enumConstant2 … }

Consider following example:
Enum FamilyMembers
{
Father,
Mother,
Daughter,
Son
}


The first constant has a default value of 0 (i.e. father = 0). Each of the following constants are given increasing integers from 0 onwards like 1, 2, 3 and so on. Note that the default type of the constants is the primitive type int. If you don’t want the integer assignments for constants to start from zero, you can manually initialize the first one from the intended starting int as follows:

Enum FamilyMembers
{
Father=1,
Mother,
Daughter,
Son
}


So, Father=1, Mother=2, Daughter=3 and Son=4. You can do the following in C#:

System.Console.WriteLine(“Order of mother is “ + (int) FamilyMembers.Mother);

However, the following is not allowed in Java:

System.out.println(“Order of mother is “ + (int) FamilyMembers.Mother);

Instead, you can associate the attribute “order” with FamilyMembers type as discussed earlier.

Coming back to C#, one can also do the following to associate average ages of members of a typical family:

Enum FamilyMembers
{
Father=50,
Mother=45,
Daughter=20,
Son=15
}


And,

System.Console.WriteLine(“Average age of a mother is “ + (int) FamilyMembers.Mother);

The base type of an enumeration, which is by default int, can infact be changed to any of the integer types. Following is the syntax to do so:

Enum FamilyMembers: short
{
Father=50,
Mother=45,
Daughter=20,
Son=15
}


Even though the base type is short, but an explicit cast is still required when using these as shown here:

short averageAge = (short) FamilyMembers.Father;

-------------------------------------------------------------------------------------
Enumerated Lists in C++

Enumerators in C++ are less complicated then structures, and they are not class-like declarations as they are in Java. Each constant in the enum type declaration is called enumerator (called enum in Java). Consider the following famous enumeration of days of the week:

enum week_days {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};

void main (){
week_days dayOne;
dayOne = Wednesday;
cout<<dayone<<endl;
}


Note that the declaration of enumeration is nothing different to what we have seen previously except for the face that the semicolon is a must in C++. Consider the following code:

enum week_days {
Sunday,
Monday=1,
Tuesday,
Wednesday=1,
Thursday,
Friday=9,
Saturday
};


In C++, enumerations are treated internally as integers which is again unlike Java, where the type of enum constant (enumerator) is its owner enum type, and nothing else. The thing to note here is that Sunday=0, Monday=1, Tuesday=2, Wednesday=1, Thursday=2, Friday=9, and Saturday=10. And so, same integer can be associated with multiple enumerators.

Since enumerators are internally treated as integers, apparently all integer arithmetic can be performed on them. Hence, the following is legal:

int i = Sunday; // i becomes 0
int j = 3 + MOnday; // j becomes 4


However, the reverse ain't possible - no implicit conversion from int to enum:

week_days dayOne = 2; // compilation error, such a type conversion is illegal.

1 comment:

akmal niazi khan said...

Programming is very interesting and creative thing if you do it with love. Your blog code helps a lot to beginners to learn programming from basic to advance level. I really love this blog because I learn a lot from here and this process is still continuing.
Love from Pro Programmer