Features and Enhancement in jdk1.5

There are a few features and enhancements that are added in jdk1.5. These are listed below:

  • Java Language Features
  • Performance Enhancement
  • Virtual Machine
  • Base Libraries
  • User Interface
  • Deployment
  • Tools Architecture
  • OS and Hardware Platforms

Java Language Features were introduced for the ease of programmers and to make it simpler. They include the following:

  • Generics
  • Enhanced for loop
  • Autoboxing/unboxing
  • Typesafe Enums
  • Varargs
  • Static import
  • Annotations

This feature of Java was introduced in 2004 and is used providing compile-time type safety,”.
Read on to find out more about it, its uses as well as disadvantages.

  • When you take an element out of a Collection, you must cast that element to the type of element that is stored in the collection.

  • Besides being inconvenient, this is unsafe. The compiler does not check if your cast is the same as the collection's type, so it can fail at run time.

  • Generics provide a way for you to communicate the type of a collection to the compiler so that it can be checked.

  • Once the compiler knows the element type of the collection, the compiler can crosscheck whether you have used the collection consistently and can then insert the correct casts on values being taken out of the collection.

  • The code using generics is clearer and safer. We have eliminated an unsafe cast and a number of extra parentheses.

When you see <Type>, read as "of type"
    For example:  ArrayList <String>al = new ArrayList<String>() ;
    Read as : al is type of String.
                  

Example of collection without Generics


Example of collection with Generics:

Note:

  1. boolean hasNext() returns true if there are more elements, otherwise it returns as false.
  2. Object next() returns the next element. It Throws NoSuchElement Exception if there is no next element present.

Why should we use Generics?

There are a number of reasons why we would use generics.

  • Stronger type checks at compile time
    • A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety.
    • Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.
  • Elimination of casts

The following code snippet without generics requires casting:

  • java.util.ArrayList.get(int index)
    The method returns the element at the specified position in this list.

  • public void add(int index, E element);
    This adds the element to the specified index in the list.

  • The enhanced for loop is a popular feature introduced with the Java SE platform in version 5.0.
  • It is also called as for each loop.

Example:

Features and Enhancements Added In Jdk1.5

  • The automatic conversion of primitive datatypes into its equivalent wrapper type is called as autoboxing. Its reverse operation is called as Unboxing.

  • As you know, we can't put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is an Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method.


Example of Autoboxing and Unboxing:

  • An enum is a data type which contains fixed set of constants.

  • It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY), or directions (NORTH, SOUTH, EAST and WEST) etc.

  • The enum constants are static and final implicitly

  • It is available from JDK 1.5. It can be thought of as a class, i.e, enum can have constructors, methods and variables.

  • Enum improves type safety; it can be easily used with a switch statement.


Example

Note:

  1. enum have static ‘values’ method that returns an array containing all of the values of the enum in the order that they are declared in.
  2. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.

Varargs are Variable Arguments which accepts none or more arguments.

  • Varargs can be used for methods that can take zero or more arguments.

  • Before varargs was introduced, we had to overload the methods.

  • If we are uncertain about the number of arguments we would need, only then should we go for varargs.


Example 1:


Example 2:

The static import feature lets fields and methods specified in a class as public static, and is used without naming the class in which the field is defined.

  • It is used for accessing static members of any class.

  • It is necessary to give the class name from where they came, if we don’t use this concept.

  • By using static import we can access static members directly, without giving the class name.

  • Static imports can be applied only to static members.


Example:

Annotations are a form of metadata which gives information about programs which is not a part of the current program.

  • It is a form of metadata.

  • It provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate. Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.

  • There are many built in annotations present and we can also create custom annotations.

    Annotations are useful in the following ways :
    1. They have information for the compiler -
      Annotations can be used by the compiler to detect errors or suppress warnings.

    2. They send Compile-time and deployment-time processing -
      Software tools can process annotation information to generate code, XML files, and so forth.

    3. Runtime processing -
      Some annotations are available to be examined at runtime.

    For example : @Override
    It assures that a subclass method is overriding the parent class method.
    If it is not so, then the compiler will give an error.

Example: