A list of new features of JDK1.5

Source: Internet
Author: User
Tags date define functions integer new features variables sin variable
An important theme of "JDK1.5" (the development of the Tiger) is to simplify development by adding features such as generics, For-each loops, automatic packaging/unpacking, enumerations, variable parameters, static import. Using these features helps us write more clear, lean, and secure code.
Here's a quick introduction to these new features. 1. Generics (Generic) C + + uses template technology to specify the element type of the collection, while Java does not have corresponding functionality until 1.5. A set can put any type of object, and we also have to force the type conversion to take objects from the collection accordingly. The Tigers introduce generics, which allow you to specify the type of elements in a collection, so that you get the benefit of strong typing for type checking at compile time.
collection<string> C = new ArrayList (); C.add (new Date ());

The compiler will give an error:

Add (java.lang.String) in java.util.collection<java.lang.string> cannot is applied to (java.util.Date)

2.for-each cyclic For-each loops are added to simplify the traversal of the set. Let's say we're going to walk through a set and do some processing on the elements. The typical code is:

void Processall (Collection c) {for (iterator i=c.iterator (); I.hasnext ();) {MyClass MyObject = (MyClass) i.next ();    Myobject.process (); }}

Using the For-each loop, we can rewrite the code to:

void Processall (collection<myclass> c) {for (MyClass myobject:c) myobject.process ();}

This code is much clearer than the above and avoids coercion of type conversions.

3. Automatic Package/unpacking (autoboxing/unboxing) automatic packaging/unpacking greatly facilitates the basic type of data and the use of their packing class. Automatic package: The basic type is automatically converted into a wrapper class. (int >> Integer) automatic unpacking: The wrapper class is automatically converted to the base type. (Integer >> int) before JDK1.5, we always worry that the collection can not store the basic type, now the automatic conversion mechanism solves our problem.
int a = 3; Collection C = new ArrayList (), C.add (a);//automatically converts to Integer.integer B = new Integer (2); C.add (b + 2);

Here the integer is automatically converted to int for addition operations, and then int is converted to integer again. 4. Enumeration (Enums) JDK1.5 has added a completely new type of "class"-enum type. For this JDK1.5 introduced a new keyword ENMU. We can define an enumeration type like this.

Public enum color{Red, White, Blue}

You can then use the Color MyColor = color.red. The enumeration type also provides two useful static method values () and valueof (). We can use them very conveniently, for example

For (Color c:color.values ()) System.out.println (c);

5. Variable parameter (VARARGS) variable parameters allow programmers to declare a method that accepts a variable number of parameters. Note that the variable parameter must be the last parameter in the function declaration. Let's say we're going 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 use overloads to do this, but this requires a lot of overloaded functions to be written, which is not very effective. If you use variable parameters, we just need one function.

public void Write (object ... objs) {for (object Obj:objs) System.out.println (obj);}

With the introduction of variable parameters, the Java reflection package is also 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 it used to be. 6. Static imports to use static members (methods and variables) We must give the class that provides this method. Static import enables all static and static methods of the imported class to be visible directly in the current class, using these static members without having to give their class names.

import static JAVA.LANG.MATH.*;.......R = sin (PI * 2); No more writing R = Math.sin (Math.PI);

However, excessive use of this feature also reduces the readability of the code to some extent.

JDK1.5 official version has been released, the latest version number: 1.5.0.01, you can download to the Sun website!


int a = 3; Collection C = new ArrayList (), C.add (a);//automatically converts to Integer.integer B = new Integer (2); C.add (b + 2);

Here the integer is automatically converted to int for addition operations, and then int is converted to integer again. 4. Enumeration (Enums) JDK1.5 has added a completely new type of "class"-enum type. For this JDK1.5 introduced a new keyword ENMU. We can define an enumeration type like this.

Public enum color{Red, White, Blue}

You can then use the Color MyColor = color.red. The enumeration type also provides two useful static method values () and valueof (). We can use them very conveniently, for example

For (Color c:color.values ()) System.out.println (c);

5. Variable parameter (VARARGS) variable parameters allow programmers to declare a method that accepts a variable number of parameters. Note that the variable parameter must be the last parameter in the function declaration. Let's say we're going 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 use overloads to do this, but this requires a lot of overloaded functions to be written, which is not very effective. If you use variable parameters, we just need one function.

public void Write (object ... objs) {for (object Obj:objs) System.out.println (obj);}

With the introduction of variable parameters, the Java reflection package is also 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 it used to be. 6. Static imports to use static members (methods and variables) We must give the class that provides this method. Static import enables all static and static methods of the imported class to be visible directly in the current class, using these static members without having to give their class names.

import static JAVA.LANG.MATH.*;.......R = sin (PI * 2); No more writing R = Math.sin (Math.PI);

However, excessive use of this feature also reduces the readability of the code to some extent.

JDK1.5 official version has been released, the latest version number: 1.5.0.01, you can download to the Sun website!


collection<string> C = new ArrayList (); C.add (new Date ());

The compiler will give an error:

Add (java.lang.String) in java.util.collection<java.lang.string> cannot is applied to (java.util.Date)

2.for-each cyclic For-each loops are added to simplify the traversal of the set. Let's say we're going to walk through a set and do some processing on the elements. The typical code is:

void Processall (Collection c) {for (iterator i=c.iterator (); I.hasnext ();) {MyClass MyObject = (MyClass) i.next ();    Myobject.process (); }}

Using the For-each loop, we can rewrite the code to:

void Processall (collection<myclass> c) {for (MyClass myobject:c) myobject.process ();}

This code is much clearer than the above and avoids coercion of type conversions.

3. Automatic Package/unpacking (autoboxing/unboxing) automatic packaging/unpacking greatly facilitates the basic type of data and the use of their packing class. Automatic package: The basic type is automatically converted into a wrapper class. (int >> Integer) automatic unpacking: The wrapper class is automatically converted to the base type. (Integer >> int) before JDK1.5, we always worry that the collection can not store the basic type, now the automatic conversion mechanism solves our problem.
int a = 3; Collection C = new ArrayList (), C.add (a);//automatically converts to Integer.integer B = new Integer (2); C.add (b + 2);

Here the integer is automatically converted to int for addition operations, and then int is converted to integer again. 4. Enumeration (Enums) JDK1.5 has added a completely new type of "class"-enum type. For this JDK1.5 introduced a new keyword ENMU. We can define an enumeration type like this.

Public enum color{Red, White, Blue}

You can then use the Color MyColor = color.red. The enumeration type also provides two useful static method values () and valueof (). We can use them very conveniently, for example

For (Color c:color.values ()) System.out.println (c);

5. Variable parameter (VARARGS) variable parameters allow programmers to declare a method that accepts a variable number of parameters. Note that the variable parameter must be the last parameter in the function declaration. Let's say we're going 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 use overloads to do this, but this requires a lot of overloaded functions to be written, which is not very effective. If you use variable parameters, we just need one function.

public void Write (object ... objs) {for (object Obj:objs) System.out.println (obj);}

With the introduction of variable parameters, the Java reflection package is also 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 it used to be. 6. Static imports to use static members (methods and variables) We must give the class that provides this method. Static import enables all static and static methods of the imported class to be visible directly in the current class, using these static members without having to give their class names.

import static JAVA.LANG.MATH.*;.......R = sin (PI * 2); No more writing R = Math.sin (Math.PI);

However, excessive use of this feature also reduces the readability of the code to some extent.

JDK1.5 official version has been released, the latest version number: 1.5.0.01, you can download to the Sun website!


int a = 3; Collection C = new ArrayList (), C.add (a);//automatically converts to Integer.integer B = new Integer (2); C.add (b + 2);

Here the integer is automatically converted to int for addition operations, and then int is converted to integer again. 4. Enumeration (Enums) JDK1.5 has added a completely new type of "class"-enum type. For this JDK1.5 introduced a new keyword ENMU. We can define an enumeration type like this.

Public enum color{Red, White, Blue}

You can then use the Color MyColor = color.red. The enumeration type also provides two useful static method values () and valueof (). We can use them very conveniently, for example

For (Color c:color.values ()) System.out.println (c);

5. Variable parameter (VARARGS) variable parameters allow programmers to declare a method that accepts a variable number of parameters. Note that the variable parameter must be the last parameter in the function declaration. Let's say we're going 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 use overloads to do this, but this requires a lot of overloaded functions to be written, which is not very effective. If you use variable parameters, we just need one function.

public void Write (object ... objs) {for (object Obj:objs) System.out.println (obj);}

With the introduction of variable parameters, the Java reflection package is also 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 it used to be. 6. Static imports to use static members (methods and variables) We must give the class that provides this method. Static import enables all static and static methods of the imported class to be visible directly in the current class, using these static members without having to give their class names.

import static JAVA.LANG.MATH.*;.......R = sin (PI * 2); No more writing R = Math.sin (Math.PI);

However, excessive use of this feature also reduces the readability of the code to some extent.

JDK1.5 official version has been released, the latest version number: 1.5.0.01, you can download to the Sun website!


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.