A programmer's tale

Thursday, July 27, 2006

Tale of enum

enum keyword is new in Java 1.5. Here I'll try to tell it's tale.

Enum is a special kind of class in java 1.5. An enum may contain some values and each value is implicitly an enum of the same type in which it is defined. Since enum is a special type of class, we can use it as a class to some extend.

It will be clear if you take an example. Let us define:

public enum Day {

   MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

}

Here, MONDAY, TUESDAY etc. are all Day enum type. In simple case you can implement the same behavior with integer, other primitive or with classes like:

public class Day {

   public static final int MONDAY == 0;
    public static final int TUESDAY == 1;
    public static final int WEDNESDAY == 2;
    public static final int THURSDAY == 3;
   public static final int FRIDAY == 4;
public static final int SATURDAY == 5;
public static final int SUNDAY == 6;

}

But you can do the thing more safely and efficiently and compact way with enum.
Now like class we can also declare variables and define methods in an enum like:

public enum DayDesc {

MONDAY("Monday is the first day"),
TUESDAY("Tuesday is the second day"),
WEDNESDAY("Wednesday is the third day"),
THURSDAY("Thursday is the fourth day"),
FRIDAY("Friday is the fifth day"),
SATURDAY("Saturday is the sixth day"),
SUNDAY("Sunday is the seventh day");

private String info;

DayDesc(String info) {
this.info = info;
}

public String info() {
return info;
}
}

In this case the enum DayDesc has a constructor and every time MONDAY, TUESDAY is declared and defined with some String info the constructor is called like a class. You can use this as follow:

public static void main(String[] args) {
System.out.println(DayDesc.MONDAY.info());
System.out.println(DayDesc.THURSDAY.info());

// .... etc
}

Here the values MONDAY, TUESDAY, WEDNESDAY etc are all constants but the private member info is not. It is like any private attribute declared or defined in normal class. You can assume that the values are like public static final attributes of a normal class. Now we write a normal class that corresponds the above enum class:

public class ClassEnum {

public static final ClassEnum MONDAY = new ClassEnum("Monday is the first day");
public static final ClassEnum TUESDAY = new ClassEnum("Tuesday is the second day");
public static final ClassEnum WEDNESDAY = new ClassEnum("Wednesday is the third day");
public static final ClassEnum THURSDAY = new ClassEnum("Thursday is the fourth day");
public static final ClassEnum FRIDAY = new ClassEnum("Friday is the fifth day");
public static final ClassEnum SATURDAY = new ClassEnum("Saturday is the sixth day");
public static final ClassEnum SUNDAY = new ClassEnum("Sunday is the seventh day");

private String info;

private ClassEnum(String info) {
this.info = info;
}

public String info() {
return info;
}
}

It's obvious that this class also can be used as the previous enum class same way. Look, the constructor of this class is private. In enum class the constructor is implicitly private, if you try to declare it as protected or public it will give you compilation error. For this reason enum class cannot be instantiated outside the enum class itself by new operator. Moreover enum class cannot be cloned. Let us see how it is made. Every java class is implicitly a super class of the class java.lang.Object. But every enum class is implicitly a super class of the class java.lang.Enum. If you look at the class, you will see that the clone() method as follows:

protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

Hence you cannot clone it. Again the reflection and serialization is specially treated for enum class so that no duplicate instance can exist. One very interesting think is that the following two methods are only generated by the compiler for enum class:
values();
valueOf(String arg0);

First method returns an array of enumerated values of that type, then you can iterate through the values like :

for (DayDesc day : DayDesc.values())
    System.out.println(day);

Second method return enum type by a String. The argument arg0 must be same to one of the values defined in enum class otherwise a java.lang.IllegalArgumentException exception is thrown.

Wednesday, July 26, 2006

Space-Time: a facinating topic to me

I don't remember when I first fallen in love, it's most probably when I was class 3 level. But I can vividly remember that I fallen in love in class 12 with the rich subject Physics.

Einstein's Theory of Relativity(Special and General) facinated me the most.
I'm not going deeper details of that. In fact I don't know the whole thing but I can feel some of them. It's a simple space-time relativeness that is simple but interesting
In Newton's physics, we khow that Space is not absolute(Why?) but Time is absolute.

Now, let a light pulse is sent from one space to another, the distance between the two spaces is relative say it be Srel and time taken by the light to go from one space to another is T.

Then speed of light c = Srel / T.
If T is absolute then the speed of light must be variable.
Einstein says that T also relative, say now it be Trel

Now the equation is c = Srel / Trel

This is just the beginning of Einstein's theory of relativity.

Thursday, July 20, 2006

Factorial of 100: Java Way

Hi,
After a long time (I think, it's long, but a galaxy says it's very short, some Leptons may say it's very long time, kidding) I'm back with a small java program that can calculate the factorial of large numbers such that 25, 30 or even 100:

public class FactorialBig {

/**
* @param args
*/
public static void main(String[] args) {
long l = 50;

System.out.println("--------BigInteger factorial-------");
System.out.println("Factorial of " + l + " = " + factorial(l));
}

public static BigInteger factorial(long number) {
return fact(BigInteger.valueOf(number));
}

private static BigInteger fact(BigInteger n) {
if (n.compareTo(BigInteger.ZERO) == 0)
return BigInteger.ONE;
else
return n.multiply(fact(n.subtract(BigInteger.ONE)));
}
}

The out put of this program is:

--------BigInteger factorial-------
Factorial of 50 = 30414093201713378043612608166064768844377641568960512000000000000