Changes in J2SE1.5

xiaoxiao2021-03-06  39

J2SE 1.5 (Development Code "Tiger") is an important modification on the Java platform and language, and records its main modifications.

Among them, the Swing API has the greatest, but there is no research on swing, so only the original record is as follows:

http://java.sun.com/j2se/1.5.0/docs/guide/swing/1.5/index.html

The original text of the overall change is as follows:

http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

Description of Language Change:

1, generics:

About Java and a good article on IBM Development

http://www-900.cn.ibm.com/developerWorks/cn/java/java_and_generics/index.shtml

SUN has a related article, Generics in the Java Programming Language:

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

2, Enhanced for loop: "Enhanced loop statement):

Code example

A typical traversal array cycle statement, 1.5 version of the previous way is:

Iterator ity (); item.hasnext ();)

{

Integer n = (Integer) Iterator.next ();

...

} // for

And can be written in version 1.5:

For (Integer N: List)

{

...

} // for

Obviously 1.5 version is greatly simplified than before, but this way of writing cannot be used when needed to modify, such as deleting elements. The reason why Java1.5 is not like C #, just as a FOREACH keyword, mainly because Sun thinks adding a special keyword cost too high (TOO COSTLY). But the 1.4 version has increased the Assert keyword, and the 1.5 version has also added ENUM keywords, so this explanation is not so convincing.

3. Autoboxing / unboxing (automatic packaging and solution):

Code example

Add an integer to an ArrayList, 1.5 version of the previous version is:

List list = new arraylist ();

List.add (New Integer (10));

And can be written in version 1.5:

List.add (10);

Because, in version 1.5, package is packaged, making it an Integer object (ie package, boxing), then join a way to an arraylist is considered unnecessary, reverse, unboxing practice. It is also necessary, such code just adds the text length of the program, so 1.5 version supports automatic packaging and unpacking operation, for Bool / Boolean, Byte / Byte, Double / Double, Short / Short, Int / Integer, Long The corresponding packaging / unpacking operations of float / float have been supported, making the code simple.

3, TypeSafe Enums: Code Example

Build an enumeration indicating the color and assign a value, you can write in version 1.5:

Public enum mycolor {red, yellow, blue}

Mycolor color = mycolor.red;

For (MyColor mycolor: mycolor.values ​​())

System.out.println (MyColor);

There is no Enum keyword in the past Java, and the 1.5 version has finally joined it, which is indeed a pleasing improvement. In addition, ENUM also provides a quaint way called VALUES () to return a collection of all values ​​of enumeration. Therefore, the output result of the above program is to output "Red", "Yellow", "Blue" branch.

The static method provided by ENUM will return a particular enumeration element in the form of a string, such as "MyColor.Valueof (" Red ")" will return "color.red". Static method Name () returns a particular enumeration element name, such as "MyColor.Red.Name ()" will return "Red". There are still many similar methods. In addition, the enum itself can have a constructor.

4, VARARGS (parameter variable method and printf):

Code example

When the number of entry parameters of a method cannot be determined, in the previous version of Java, the usual approach is to transfer multiple parameters in a array or object collection as a parameter, and 1.5 version previously written is:

Int sum (Integer [] NumBers)

{

INT nsum = 0;

For (Int i: number)

NSUM = I;

Return nsum;

}

...

/ / Call this method elsewhere

SUM (new integer [] {12, 13, 20});

And can be written in version 1.5:

Int sum (Integer ... Numbers)

{

INT nsum = 0;

For (Int i: number)

NSUM = I;

Return nsum;

}

...

/ / Call this method elsewhere

SUM (12, 13, 20);

Obviously, the version of the 1.5 version is more simple, and more intuitive, especially the method of calling statements, not only simplifies a lot, but also more conforming to usual way of thinking, it is easier to understand.

The 1.5 version itself has a typical example of which is a typical example of the C style, the format output method of the C style --Printf.

Code example

Output an additional calculation, 1.5 version of the previous way is:

INT x = 5;

INT Y = 7;

INT NSUM = X Y;

System.out.println (x " " y "=" nsum);

And can be written in version 1.5:

System.out.printf ("% D % D =% D / N", x, y, nsum);

The output structure of the above two ways is the same, ie "5 7 = 12".

This change is not only the form, but the printf can also provide more flexible, powerful output functions, such as the output according to the two integers, can be written as "system.out.printf ("% 02d % 02D = % 02D / N ", x, y, nsum);", the output result will be "05 07 = 12". 5, static import (static reference):

Code example

When we have to get an item number, the 1.5 version of the previous way is:

Import java.lang.math; // out of the program

...

Double x = math.random ();

And can be written in version 1.5:

Import static java.lang.math.random; // out of the program

...

Double x = random ();

Static reference allows us to call an introduction method like a local method. When we need to introduce multiple methods of the same class, it is only necessary to write "import static java.lang.math. *". Such a reference method is equally effective for enumerations.

6, metadata (annotation) (metadata):

转载请注明原文地址:https://www.9cbs.com/read-76856.html

New Post(0)