Java 5 Features – Enum and Annotation

Intent

I want to summarize some new and interesting Java 5 features in this article and how they change the way I code.

Enum

I use int constants to make my life easier b/c it can avoid typo. However, it has several drawbacks:

  1. Java doesn’t provide namespace for int enum groups. I can either prefix my constant like ABC_ or using inner interfaces to organize it.
  2. It is compile-time constants. So you need to recompile once changed.
  3. No easy way to translate int enum constants into printable string during debugging.
  4. You cannot iterate over all the int enum easily.
  5. You need a way to validate the enum is an valid int

Use new enum type in Java 5:

public enum Apple {FUJI, PIPPIN, GRANNY_SMITH}

Enum is full-fledged final class that export one instance for each enumeration constant via a public static final field.

  1. Namespace is provided via the enum type name.
  2. You can reorder and add the enumeration constant without recompiling its client.
  3. You can translate enum into printable strings via toString() method.
  4. Enum type provides you values() method to iterate your enumeration constants (based on declaration order).
  5. Type-checking can be used for the validation check
  6. You can associate data with enum constant
  7. Enum is immutable, serializable and comparable.

EnumSet

If elements of an enumerated types are used primarily in sets, it is traditional to use the int enum pattern, assigning a different power of 2 to each constant like READ = 1 << 2, WRITE = 1 <<1, EXECUTE = 1 << 0 to represent permissions per each entity in Unix. This representation lets you use the bitwise OR operation to combine several constants into a set, known as a bit field. The bit field representation also lets you perform set operations such as union and intersection efficiently using bitwise arithmetic. But bit fields have all the disadvantages of int enum mentioned above.

Now, java.util package provides the EnumSet to efficient represent sets of value drawn from single enum type. This class implements Set interface and internally use bit vector to represent set of values. For example, if you enum types has 64 values, the entire EnumSet can be represented as a single long, so its performance is comparable to the bit field.

The EnumSet class provides three benefits a normal set does not:

  1. Various creation methods that simplify the construction of a set based on an Enumeration
  2. Guaranteed ordering of the elements in the set based on their order in the enumeration constants are declared
  3. Performance and memory benefits not nearly possible with a regular set implementation

Annotation

An annotation is a new language feature introduced in J2SE 5.0. Simply put, annotations allow developers to mark classes, methods, and members with secondary information that is not part of the operating code.You can see annotation is a way to extend Java language.

Before annotation from Java 5, you may use naming patterns to indicate that some program elements like method demanded special treatment by a tool or a framework. Like JUnit required its users to name the test methods with the pattern like testXXX(). It works but with some big disadvantages:

  1. Typo problem
  2. It doesn’t provide a way to associate parameter values with program elements.

Annotation can solve this problem. To use it, you can:

  1. Create you own marker annotation (@interface is the keyword) or parametized annotation. You can annotate the annotation (ie. meta-annotation). Example: @Retention and @Target. And marker annotation has no parameter associated with it.
  2. Annotate the program elements
  3. Write processor to handle your annotated code. Generally, annotations never change the semantics of the annotated code, but enable it for special treatment by tools. Now, the metadata of Method carries additional info for your job. You can use Method’s isAnnotationPresent() to check if a method is annotated by certain annotation type. If you annotation carried parameter, you can use Method’s getAnnotation() to get the Annotaton object and use value() to obtain the parameter.

Reference

Below are some related articles I feel useful:

  1. http://www.javalobby.org/java/forums/t16967.html
  2. Annotation in Tiger – Part 1 Meta-Annotation
  3. Annotation in Tiger – Part 2 Custom Annotation

 

 

Comments

comments

, , ,

Subscribe

Subscribe my "7 Days Crack Course" to make money online together! Free for the first 100 registration.

No comments yet.

Leave a Reply