The Tigers: The new language features of Java 5.0 __java

Source: Internet
Author: User
Tags wrapper
Author: Gong Fei

In the second half of 2004, Sun released J2SE 5.0, code-named Tiger, unveiling an important milestone in Java development. In the past Java upgrades have been more of a library function improvement, and this time directly from the grammatical level of the enhancement. Jump directly from 1.4 to 5.0 (Sun was originally intended to use the version of 1.5.0), only from the change in version number can be seen the strength of this upgrade is so great. So, what has changed? Please follow me one or two (the code examples cited are excerpted from the J2SE 5.0 in a nutshell):

fan Type (generics)

Previously, we needed to establish corresponding methods, classes, or interfaces for different data types, such as an addition method add may need to define int add (int a, int b) Separately, string add (string A, string b), MyClass Add ( MyClass A, MyClass b), and so on, even though the processing logic in these methods is exactly the same (except for different data types).
Like the template in C + + (template), generics enable programmers to create common methods, classes, and interfaces, in which case the type of data they manipulate is specified by parameter. By using a paradigm, you can automatically work with different data types by creating only one class. As a result, the paradigm extends the ability of programmers to reuse code. In addition, the paradigm also increases the type safety. After using the paradigm, we no longer need an explicit cast (cast), so that the type does not match at compile time, avoiding type conversion errors at run time.
Here is the code comparison before and after using the paradigm:
Before using the model:
ArrayList list = NewArrayList (); List.add (0, New Integer(42)); intTotal = ( Integer) list.get (0)). Intvalue ();
After using the paradigm (below there are more concise code to take advantage of the automatic boxing/unboxing feature):
arraylist< Integer> list = Newarraylist< Integer> (); List.add (0, New Integer(42)); intTotal = List.get (0). Intvalue ();

Incidentally, it is regrettable that operator overloading has not been able to be joined in with the paradigm. If Java was able to support operator overloading, it would feel better to use the paradigm (though, Java is becoming more and more like C + +).

Meta data (Metadata)
New metadata tools are added for future consideration, allowing you to embed annotations (annotation) in your program, which can be handled by different programming tools. For example, a tool can generate Java source code based on the requirements of annotations (annotation) so that as long as the programmer specifies an action, it can then leave the actual provision of the code to the tool, greatly reducing the number of repetitive code that the programmer must manually enter.
The following is a comparison of code before and after using meta data:
Before use:
Public InterfacePingif extendsRemote { Public voidPing () throwsRemoteException; } Public classPing ImplementsPingif { Public voidPing () {...}}
After use:
Public class Ping {
Public @remote void Ping () {
......
}
}


Automatic Boxing (autoboxing) and automatic unboxing (auto-unboxing)
Since the birth of Java, the simple data type (int,long,float, etc.) and its corresponding packaging type (integer,long,float, etc.) has not been automatically converted between, which brings us a lot of trouble. Now Java has finally added automatic boxing (autoboxing) and automatic unboxing (auto-unboxing), for us to solve this problem. The automatic boxing (autoboxing) feature enables Java to automatically wrap a simple data type (such as int) into the corresponding wrapper type (for example, Integer). Automatic unboxing (auto-unboxing) is the reverse process of automatically converting a wrapper type (for example, Integer) to its corresponding simple data type (for example, int). Examples are as follows:
before:
arraylist< Integer> list = Newarraylist< Integer> (); List.add (0, New Integer(42)); intTotal = (list.get (0)). Intvalue ();
Later (please refer to the generic sample code before using the generic section):
arraylist< Integer> list = Newarraylist< Integer> (); List.add (0, 42); intTotal = list.get (0);

enumeration (enumeration)
Many programming languages have enumerated types, and when Java was born, the Java creator didn't include this stuff in Java, which caused us to write a lot of public static final in the program. Now the enumeration is finally added to Java.
In essence, an enumeration is a list of named constants. The enumeration type is supported by a new keyword enum. The following is an example code that defines an enumeration:
PublicEnum stoplight {red, amber, green};
By the way, it is a pity that constants are not joined with generics, which means that the fate of using public static final is not over.


Enhanced for Loop
A loop in the form of "For-each" is added to Java 5.0. This enhancement facilitates a large number of loops for collections, arrays, and so on, and provides useful help in preventing arrays from crossing the bounds, and avoids the mandatory type conversions (case) that were required, so that we can find the type mismatch at compile time and avoid type conversion errors at runtime.
The following is a code comparison before and after using the new for loop:
Before use:
arraylist< Integer> list = Newarraylist< Integer> (); for(Iterator i = List.iterator (); I.hasnext ();) { IntegerValue= ( Integer) I.next (); ...... }
After use:
arraylist< Integer> list = Newarraylist< Integer> (); for( Integeri:list) {...}

indefinite 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 used the method of wrapping parameters into an array or collection. Now there are varargs to help us solve the problem. VarArgs lets a method have a variable number of arguments. The addition of varargs makes it easier to create a method with variable number of parameters. The following are examples of using indeterminate parameters:
Method definition voidArgtest ( Object... args) { for( intI=0;i <args. length; i++) {}}//Call mode Argtest ("Test", "data");

statically imported ( static import)
Using static members in the past we usually need to make this form of invocation: Yourclassname.staticmember, each time we use a static member, we have to write the name of the class that the static member belongs to. Now static import lets us not write the names of classes every time, we can access them directly through the names of static members. The following are examples of using static imports:
Static Import Import Staticjava.awt. BorderLayout.*; Call static member Getcontentpane (). Add ( NewJPanel (), CENTER);

Other Changes
The above list is primarily a language-level upgrade of some Java 5, and Java 5 has also been upgraded in a number of other areas such as the base class library, user interface, virtual machines, and execution efficiencies. For example, in the Basic class library, the collection framework was updated for the appearance of the paradigm, the synchronization of threads was improved for the convenience of multithreading development, and new formatter and scanner classes were added to facilitate the input and output.
I am not going to enumerate these aspects here, if you need to find the appropriate documents to learn.

Related Article

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.