In this article, we will discuss several new language features in JDK1.5, including:
Generics - COLLECTIONSFEs provide compile time type safety, no need to make an object from Collectes to get an object (CAST)
Enhanced "For" loop - Reduce the potential error of iterator (ERROR-PRONENESS)
AutoBoxing / Unboxing - No need to convert between the primitive type (such as a double ", such as a wrapper type (for example, Double).
TypeEnums - Provides the benefits of type security enumeration mode.
Static Import - No need to prefix the static member variables using other classes. This will make the code more concise.
Metadata - Make programmers to write the Boiler Plate Code and provide opportunities for declarative programming.
Let us discuss each new feature in detail and see some examples.
Generics
The generic is the most "cool" feature in JDK1.5. By introducing generics, we will throw ClassCastExceptions minus ClassCastExceptions when you get compile time. In JDK1.5, you can declare a collection of objects that will receive / returned. In JDK1.4, create a list of employee names (list) requires a collection object, like the following statement:
List listofemployeename = new arraylist ();
In JDK1.5, you will use the following statement
List
The most "cool" is that if you try to insert a non-String type value, you will find and fix this problem at compile. There is no generic, you will find such a bug, tell you when your customer calls, the program you have written throws the ClassCastException exception and crashes.
In addition, you don't have to force conversions when you get an element from the collection. Therefore, it is:
String Employeename = (String) ListofemPloyee.get (i)); the following statement will be more simpler than the above:
String Employeename = ListofemPloyee.get (i);
Unclear objects and forced conversion objects are unreasonable, and more importantly, it will fail at runtime. If the user is unintended to enter a collection containing the String Buffers, the result will be. In Listing A, the customer is required to pass a compiler that cannot force the Strings type collection. The same method is displayed in Listing B. How to use generics is implemented.
Listing a
StaticBooleanCheckName (Collection EmployeenameList, String Name) {
ITERATORI = EmployeenamList.iterator (); I.hasnext ();) {string s = (string) i.next ();
IF (s.equals (name)) {
Return True;
// Print Employee Name Here ...
}
}
Return False;
}
Listing B
StaticBooleanCheckName (Collection
Iteratori = EMPLOYENAMLIST.ITERATOR (); I.hasNext ();) {
IF (i.next (). Equals (name)) {
Return True;
// Print Employee Name Here ...
}
}
Return False;
}
Now, through the method signature, you can clearly know that the input set must only contain Strings. If the customer tries to get into a collection containing String Buffers, the program will not be compiled. At the same time, the method does not contain any forced conversion. It only needs a short one, once you are used to generic, it is more clear.
The FOR cycle syntax under the current version of JDK is as follows:
Void Printall (Collection C) {for (Iteratori = C.ITerator (); I.hasNext ();) {Employee EMP = (Employee) i.next (); System.out.Println (Emp.getName ()); }
Now, use the enhanced for statement to implement the same method:
Voidprintall (Collection C) {for (Object O: c) System.out.println ((Timertask) o) .getname ());}
In such a For loop, you should ":" as "in", so in this case "for Object O IN C" can be seen. You can find this for loop more readable.
AutoSol / Unboxing
Java has basic data types, and there is a packaging class around these basic data types. Typically, programmers need to convert one type to another. Take a look at the code snippet in Listing C.
Listing C
PUBLIC CLASS EMPLOYEEEE {
Private static final integer = new integer (0);
Public static void main (string args []) {
// code for adding n to an integer
INT n = 10;
Integer Age = New Integer (30);
Integer AgeafTenyear = New Integer (age.intValue 10);
}
}
Please note that how messing with the internal cycle code used to calculate AgeafTenyear. Now, look at the same program in Listing D. Use Autoboxing to rewrite the appearance.
Listing D
PUBLIC CLASS EMPLOYEEEE {
Public static void main (string args []) {
INT n = 10;
Integer Age = New Integer (30);
Integer AgeafTenyear = age 10;
}
}
One thing is worth noting: In the previous, if you take it out, it will become 0. In the second code, the compiler will automatically convert Integer for INT and then add 10, then convert it back to INTEGER.
Type security enumeration (TypeEnums)
Type security enumeration provides the following features:
They provide time for compile time.
They are all objects, so you don't need to put them in the collection.
They are implemented as a kind of implementation, so you can add some methods.
They provide appropriate namespaces for enumeration types.
They printed with information (Informative) - if you print an integer enumeration (Intenum), you just see a number, it may not have information.
Example 1:
ENUM season {winter, spring, summer, fall}
Example 2:
Public enum coin {Penny (1), Nickel (5), DIME (10), quarter (25); coin (int value) {this.value = value;} private final int value; public int value () {Return Value }}
Static import (static import)
The static introduction is easier to read. Often, you have to use constants defined in another class, like this:
Importorg.yyy.pkg.increment; Class Employee {Public Double Calculatesalary (Double Salry {Return Salary;})
At that time, use static imports, we need to use these constants for constant preceding prefix class names, like this:
Import static org.yyy.pkg.increment; class employee {public double calculatesalary (double sales {return salary increment * Salary;}
Note that we can call Increment, do not use class name increment ..
Metadata
Metadata feature allows developers to make more easier development with tools provided by vendors. Take a look at the code in the Listing E..
Listing E
Importorg.Yyyy.hr;
Public interface Employeei Extends Java.rmi.Remote {
Public string getName ()
Throwsjava.rmi.RemoteException;
Public string getLocation ()
Throwsjava.rmi.RemoteException;
}
Public Class EmployeeImpl IMPLEments Employeei {
Public string getname () {
}
Public string getLocation () {
}
}
With the support of metadata, you can rewrite the code in Listing E:
Importorg.Yyy.hr; public class employee {@Remote public string getName () {...} @Remote public public string getLocation () {...}} is as seen by you, all models of code Be gone.
These new features and specifications will be implemented in JDK1.5. It will provide more choices for Java programming communities to write robust, scalable code. Serious Java programmers will feel good to familiarize with this Java programming language.