New Features of jdk1.5

Source: Internet
Author: User
[Switch] New Features of jdk1.5

One important topic of "jdk1.5" is to simplify development by adding some features, including generics, for-each loops, automatic package installation/unpacking, enumeration, and variable parameters, 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)
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. JDK introduces generics that allow you to specify the type of elements in the set. In this way, you can obtain the benefits of checking strong types at the time of compilation.

II. 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();
    }
}


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();
}

Iii. 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 );


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.

Note: Is there any problem with automatic unpacking of null integers?

Iv. 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
}


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);


for (Color c : Color.values())
            System.out.println(c);

V. 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);


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);
}


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.

Note: Does a function that supports variable parameters support polymorphism?
Vi. 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 );


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.