Tiger out of the mountain - New Language Characteristics of Java 5.0

xiaoxiao2021-03-06  41

Author: male fly

In the second half of 2004, Sun released J2SE 5.0 that developed codenamed "Tiger", unveiled an important milestone for Java development. More in the past Java upgrades are improvements to some library functions, and this time they have been enhanced from the grammar level. Jump directly from 1.4 to 5.0 (Sun is originally intended to use 1.5.0 version number), and you can see that this upgrade is so large if you change from version number changes. So, what changes do you change? Please feel followed by me, and the code example is taken from "J2SE 5.0 in a nutshell":

Generics

In the past, we need to establish a corresponding method, class or interface for different data types, such as an addition method ADD may need to define int Add (INT A, INT B), String Add (String A, String B), Multiple methods such as Myclass Add (MyClass A, MyClass B), even if the processing logic in these methods is exactly the same (just different data types).

Like Template in C , the model allows programmers to create generic methods, classes, and interfaces. In this case, the type of data they operate is specified by parameters. By using a model, you can automatically work in different data types only if you create a class. Therefore, the model expands the ability of programmers to multiplexed code. In addition, the model has also increased types of safety. After using the model, we no longer need explicit forced conversion (CAST), which can find type conversion errors when compiling, avoiding the type conversion error when running.

Below is the code comparison before and after using the model: use the model:

ArrayList List = New ArrayList ();

List.Add (0, New Integer (42));

INT TOTAL = ((Integer) list (0)). INTVALUE ();

After using the model (below, you will further use the automatic packing / split characteristics.):

ArrayList list = new arraylist ();

List.Add (0, New Integer (42));

INT TOTAL = list.get (0) .intValue ();

By the way, it is very unfortunate that the operator overload is not able to join with the model. If Java can support operator overload, it feels better when using the model (however, Java is getting more and more like C ).

Metadata

New metadata tools are more increased for future considerations, which allows you to embed an innotation in the program, which can be processed by different programming tools. For example, the tool can generate a Java source code according to the annotation requirements, so as long as the programmer specifies a action, then you can leave the actual code to provide the tool, thereby greatly reduced the programmer must manually enter repetition. The number of code.

Below is a comparison of code before and after metadata: Using: public interface pingif extends remote {

Public void ping () throws receexception;

}

PUBLIC CLASS PING IMPLEMENTS PING {Public Void Ping () {

......

}

} After use: public class ping {

Public @Remote void ping () {

......

}

}

Autoboxing and automatic unpacking (auto-unboxing)

Since the birth of Java, simple data type (int, long, float, etc.) and its corresponding packaging type (Integer, Long, Float, etc.) have been automatically converted, which has brought us a lot of trouble. Now Java has finally added autoBoxing and auto-unboxing, which solves this problem for us. AutoBoxing features Let Java automatically package a simple data type (for example, int) to the corresponding package type (eg, Integer). Auto-unboxing is the opposite process, which is to automatically convert a packaging type (for example, Integer) to its corresponding simple data type (for example, int). For example, as follows:

before:

ArrayList list = new arraylist ();

List.Add (0, New Integer (42));

INT TOTAL = (List.get (0)). INTVALUE ();

In the future (please use the example code before using the model):

ArrayList list = new arraylist ();

List.add (0, 42);

INT TOTAL = list.get (0);

Enumeration

Many programming languages ​​have an enumeration type, while Java is born, Java's creator did not include this static in Java, which caused us to write a lot of public static final in the program. Now that enumeration is finally added to Java.

Essentially, an enumeration is a list of name constants. Enumeration types are supported by new keyword enum. Here is an example code that defines an enumeration:

Public enum stoplight {red, amber, green};

In the case, it will be mentioned, very regret, constant is not able to join with the model, which means that the fate of public static final has not ended yet.

Enhanced for loop

The "for-Each" form in Java 5.0 has been added. This enhancement provides convenience for a large number of collections, array, and the like, and to prevent the array of crossed help, it also avoids the necessary forced type conversion (case), so that we can find the type when compiling, Avoid the type conversion error when running. .

Below is the code comparison before and after using the new for loop: use before: arraylist list = new arraylist ();

Iterator i = list.iterator (); i.hasnext ();) {

Integer value = (integer) i.next (); ......

} After use: arraylist list = new arraylist ();

For (Integer i: List) {

......

}

Uncertain parameters (VARARGS)

In the actual development process, sometimes the method parameters are not fixed. In the past, in order to solve the problem, we often use ways to pack parameters into a array or collection. There are now Varargs to help us solve this problem. VARARGS allows a method to bring variable number of parameters. The addition of VARARGS makes it easier to create a variable number of variable parameters. Below is an example of using an uncertain parameter:

// method definition

Void Argtest (Object ... args) {

For (int i = 0; i

}

}

// call mode

Argtest ("Test", "DATA");

Static import (static import)

In the past, static members usually we need to make this form of call: YourClassName.StaticMember, we must write the name of this static member to the name of this static member when we use static members. Now static imports allow us to write the names every time, you can access them directly through the name of the static member. Here is an example of using static imports:

// Static import

Import static java.awt.BorderLayout. *;

// call static members

GetContentPane (). add (new jPanel (), center;

Other changes

The main one listed above is some Java 5 upgrades in the language level, Java 5 has also been upgraded in other aspects of the basic class library, user interface, virtual machine, execution efficiency. For example, in terms of basic class libraries, the set frame is updated, and threads have been improved in order to facilitate multithreading development, which adds new Formatter and Scanner classes to facilitate input and output.

I will not listen to these aspects, if you need to find the corresponding document for learning.

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

New Post(0)