One important topic of "JDK1.5" is to simplify development by adding features, including generic, for-each loop, automatic package installation/unpacking, and enumeration, variable Parameter, static import. Using these features helps us write code that is clearer, lean, and secure.
Next we will briefly introduce these new features.
1. Generic (Generic)
C ++ can specify the element type of the set through the template technology, while Java has never had the corresponding function before 1.5. An object of any type can be put in a set. When we take objects from the set, we have to forcibly convert them. Tigers introduce generics, which allow you to specify the element types in the set, so that you can get the benefit of checking strong types at compile time.
Collection c = new ArrayList ();
C. add (new Date ());
The compiler will give an error:
Add (java. lang. String) in java. util. Collection cannot be applied to (java. util. Date)
2. For-Each loop
The For-Each loop must be added to simplify the traversal of the set. Suppose we want to traverse a set to process the elements. The typical code is:
Void processAll (Collection c ){
For (Iterator I = c. iterator (); I. hasNext ();){
MyClass myObject = (MyClass) I. next ();
MyObject. process ();
}
}
With the For-Each loop, we can rewrite the code:
Void processAll (Collection c ){
For (MyClass myObject: c)
MyObject. process ();
}
This code is much clearer than above and avoids forced type conversion.
3. Autoboxing/unboxing)
Automatic package installation/unpacking greatly facilitates the use of basic data and their packaging.
Automatic package installation: the basic type is automatically converted to the packaging class. (int> Integer)
Automatic package Splitting: the package type is automatically converted to the basic type. (Integer> int)
Before JDK1.5, we always worried that the set cannot store basic types. Now the automatic conversion mechanism solves our problem.
Int a = 3;
Collection c = new ArrayList ();
C. add (a); // It is automatically converted to Integer.
Integer B = new Integer (2 );
C. add (B + 2 );
Here, Integer is automatically converted to int for addition, and then int is converted to Integer again.
4. enumeration (Enums)
JDK1.5 adds a brand new type of "class"-Enumeration type. Therefore, JDK1.5 introduces a new keyword enmu. We can define an enumeration type in this way.
Public enum Color
{
Red,
White,
Blue
}
Then we can use Color myColor = Color. Red.
The enumeration type also provides two useful static methods: values () and valueOf (). We can easily use them, for example
For (Color c: Color. values ())
System. out. println (c );
5. variable parameters (Varargs)
Variable parameters allow programmers to declare a method that accepts variable numbers of parameters. Note: variable parameters must be the last parameter in the function declaration. Suppose we want to write a simple method to print some objects,
Util. write (obj1 );
Util. write (obj1, obj2 );
Util. write (obj1, obj2, obj3 );
...
Before JDK1.5, we can implement it with reloads, but this requires writing a lot of overload functions, which is not very effective. If variable parameters are used, we only need one function.
Public void write (Object... objs ){
For (Object obj: objs)
System. out. println (obj );
}
After variable parameters are introduced, Java reflection packages are more convenient to use. For c. getMethod ("test", new Object [0]). invoke (c. newInstance (), new Object [0]). Now we can write c. getMethod ("test "). invoke (c. newInstance (), this code is much clearer than the original one.
6. Static import (Static Imports)
To use static members (methods and variables), we must provide the class that provides this method. Static import allows all static variables and static methods of the imported class to be directly visible to the current class, without having to give their class names.
Import static java. lang. Math .*;
.......
R = sin (PI * 2); // No need to write r = Math. sin (Math. PI );
However, over-using this feature will also reduce code readability to a certain extent.
The official JDK1.5 version is coming soon. Let's look forward to it together. We can go