An important topic of "JDK1.5" (development code tiger) is to simplify development by adding some features, including generics, for-Each cycles, automatic packages / unpacking, enumeration, variable parameters, static Import. Use these features help we write more clear, fine, safe code.
Let's take a brief introduction to these new features. 1. Generic C can specify a collection of element types by template technology, while Java has no corresponding function before 1.5. A collection can place any type of object, and we have to enforce the type conversion from the item from the collection. Tiger introduces generics, which allows you to specify the type of elements in the collection so that you can get the benefits of type checking at compile time.
Collection
The compiler will give an error:
Add (java.lang.string) in java.util.collection
2.For-Each Cycle For-Each Cycle Add to simplify the traversal of the collection. Suppose we want to traverse a collection of elements for them. Typical code is:
Void Processall (Collection C) {for (Iterator I = C.Iiterator (); I.hasNext ();) {MyClass MyObject = (MyClass) i.next (); myObject.process ();}}
Using the for-Each loop, we can rewrite the code into:
Void Processall (Collection
This code is much more clear than above, and avoids mandatory type conversion.
3. Autoboxing / UnboXING Auto Package / Unpacking is greatly convenient for basic type data and their packaging. Automatic packing: The basic type is automatically turned into a packaging class. (INT >> Integer) Automatic unpacking: The packaging class is automatically converted to the basic type. (Integer >> Int) Before JDK1.5, we always not store the basics for collection Type and worry, now the automatic conversion mechanism solves our problems.
INT A = 3; Collection C = New ArrayList (); C.Add (a); // Auto Convert to Integer.integer B = New Integer (2); C.ADD (B 2);
Here Integer is automatically converted to int for an additional operation, and then INT is again converted to Integer.
4. Enumeration (Enums)
JDK1.5 joins a new type of "class" - enumeration type. To this end, JDK1.5 introduces a new keyword ENMU. We can define an enumeration type.
Public enum color {red, white, blue}
You can then use the color mycolor = color.red.
The enumeration type also provides two useful static methods values () and valueof (). We can use them easily, for example
For (Color C: color.values ()) System.out.println (C);
5. Variable parameters (VARARGS)
Variable parameters allow programmers to declare a way to accept variable number parameters. Note that variable parameters must be the last parameter in the function declaration. Suppose we have to write a simple way to print some objects, util.write (obj1); util.write (obj1, obj2); util.write (obj1, obj2, obj3); ...
Before JDK1.5, we can implement it with overload, but this needs to write a lot of overload functions, which is not very effective. If we use variable parameters, we only need a function.
Public void write (object ... objs) {for (Object Obj: objs) system.out.println (obj);
After introducing variable parameters, Java reflective 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 cleared.
6. Static Imports
To use static members (methods and variables) we must give classes that provide this method. Using static imports can make all static variables and static methods imported into the current class directly, using these static members do not need to give their class names.
Import static java.lang.math. *; ...... .r = sin (pi * 2); // No need to write R = Math.sin (Math.pi);
However, excessive use of this feature will also reduce the code-readability to a certain extent.