January 10, 2016

ALL ABOUT ENUM IN JAVA


  • a.  Enums were introduced as new type in java in jdk1.5. prior to that there was concept of classes and interfaces(as type) only. But these were not sufficient when we need to define a finite set of allowed values for a specific data type. So enumerated type concept was introduced and it was known by the name enum.
  • b.  Enums allow you to dump most of your “public static final” variable declarations.
  • c.  Enums are type like class: As a result, we get type-safety, compile-time checking, and the ability to use them in variable declarations. This overcome the problems that we have faced while using public static final integer constants.
  • d.  Enums extend java.lang.Enum : java.lang.Enum was a new class in jdk1.5, and is not itself an enumerated type. All enumerated types implicitly extend Enum.
  • e.  Enumerated types aren’t integers :Each declared value is an instance of the enum class itself; this ensures type-safety and allows for even more compile-time checking.
  • f.  Enums have no public constructor :This removes the ability to create additional instances of the enum not defined at compile-time. Only those instances defined by the enum are available.
  • g.  Enum values are public, static, and final :Values cannot be overwritten, changed, or otherwise messed with in ways that affect your programming logic. The enum itself is effectively final, as it cannot be subclassed.
  • h.  Enum values can be compared with == or equals( ) :Because enums are effectively final, and there is a distinct set of values, you can use—for comparison. Additionally, enumerated types have a working equals( ), for use in collection classes.
  • i.  Enums implements java.lang.Comparable :As a result, enum values can be compared with compareTo( ), and ordering occurs in the same order as values are declared in the enum declaration.
  • j.  Enums override toString( ) :The toString( ) method on an enumerated type returns the name of the value.
  • k.  Enums provide a valueOf( ) method :The static valueOf( ) method complements toString( ). If you change the behavior of toString( ), you need to also change the behavior of valueOf( ). These two methods should always be mirror images of each other.
  • l.  Enums define a final instance method named ordinal( ) :oridinal( ) returns the integer position of each enumerated value, starting at zero, based on the declaration order in the enum. Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such java.util.EnumSet and java.util.EnumMap
    m. Enums define a values( ) method : values( ) allows for iteration over the values of an enum.

Notes: As Enum is a type in java, then can we use enum with collection API in java?

Answer: We can use enum with collection API as EnumMap and EnumSet.

Some points regarding EnumMap

1)  public class EnumMap, V> extends AbstractMap<K, V> implements java.io.Serializable, Cloneable : it shows key for enummap can be of enum type.
2)  Enum maps are represented internally as arrays, in an array implementation, the ordinal value of each enumerated type constant serve as the index of the corresponding value.
3)  An iterator over the key set takes time proportional to number of constants in the enum type and returns the keys in their natural order (the order in which the enum constants are declared)
4)  Iterators returned by the collection views are weakly consistent that means they will never throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.
5)  Like most collection implementations EnumMap is also not synchronized(not thread-safe).
6)  Code example :

package part_One;

import java.io.IOException;
import java.io.PrintStream;
import java.util.EnumMap;

enum BuildProcess {
INITIALIZING, COMPILING, COPYING, JARRING, DONE, ERROR
}
public class E {

public E() {
}
public void testingEnumMap(PrintStream out) throws IOException {
// Create a map with the key and a String message
// focus on contructor( passed the .class file)

EnumMap<BuildProcess, String> buildMessages = new EnumMap<BuildProcess, String>(
BuildProcess.class);
// Initialize the map
buildMessages.put(BuildProcess.INITIALIZING, "Initializing Ant...");
buildMessages.put(BuildProcess.COMPILING, "Compiling Java classes...");
buildMessages.put(BuildProcess.COPYING, "Copying files...");
buildMessages.put(BuildProcess.JARRING, "JARring up files...");

buildMessages.put(BuildProcess.DONE, "Build complete.");
buildMessages.put(BuildProcess.ERROR, "Error occurred.");

// Iterate and print messages
// focus on values() method
for (BuildProcess status : BuildProcess.values()) {
out.println("For status " + status + ", message is: "
+ buildMessages.get(status));
}
}

public static void main(String[] args) {
try {
E tester = new E();

tester.testingEnumMap(System.out);
} catch (IOException e) {
e.printStackTrace();
}
}
}