jdk1.5 new Features

Source: Internet
Author: User

1. Enhanced for Loop

the reason for introducing an enhanced for loop : In previous versions of JDK5, iterating over the elements in an array or collection requires first getting the length of the array or the iterator of the collection, which is troublesome! Therefore, a new syntax-enhanced for loop-is defined in JDK5 to simplify such operations. Enhanced for loops can only be used in arrays, or implemented iterable interface on the collection class

 for (Variable type  variable: An array or collection that needs to be iterated) {}

To enhance the details of the FOR loop:

1. Iterators can manipulate traversed elements and cannot manipulate elements in the collection when using an enhanced for loop.

2. Add the For loop difference from the normal for loop.

3. The traversal of the map.

2. Variable parameters

 Public Static intSUM2 (int... numbers) {        if(Numbers = =NULL) {System.out.println ("The value of the mutable parameter is null"); return0; }        if(Numbers.length = = 0) {System.out.println ("The value of the variable parameter has a length of 0"); return0; }        intsum = 0;  for(intnum:numbers) {Sum+=num; }        returnsum; }

Details of variable parameters

Statement:

In a method, there can be at most one mutable parameter.

mutable parameters can only be placed on the last side of the parameter list.

Call:

When using variable parameters, you can pass 0 or more parameters.

When you use variable parameters, you can also pass an array in, representing multiple parameters.

Use:

When used inside a method, an array is used.

When no arguments are passed on the call (0 passes), the parameter array inside the method has a value (not null), but the length is 0.

3, automatic packing, unpacking

Auto-Boxing: A developer can assign a basic data type directly to the corresponding wrapper class.

Automatic unpacking: A developer can assign a wrapper class object directly to the corresponding base data type.

Typical applications:

New ArrayList ();    List.add (1);     // List.add (New Integer (1));    int i=list.get (0);     // int J = (Integer) list.get (0);

4. Enumeration class

Some methods at run time, the data it needs can not be arbitrary, but must be a certain range of values, such problems before JDK5 with the custom with the enumeration function of the class to solve, JAVA5 can be directly used to solve the enumeration.

For example: traffic lights (red, yellow, green) sex (male, female) week (Monday, three ...)

Fractional Grades (A, B, C, D, E)

The new enum keyword for JDK 5 defines an enumeration class.

Implementation of enumerations

To define an enumeration class by using an enum

Defining enumeration values in an enumeration class (uppercase)

enum Gender {    MALE, FEMALE;}

Discover that each of these enumeration values is a concrete instance object of the enumeration class. Just static constants.

An enumeration class has the following characteristics:

The enumeration class is also a special form of Java class.

Each enumerated value declared in an enumeration class represents an instance object of an enumeration class.

As with normal classes in Java, you can declare properties, methods, and constructors when declaring an enumeration class.

 Public classDemo1 { Public Static voidMain (string[] args) {Gender male=Gender.male;    System.out.println (Male.getinfo ()); }}enumGender {MALE (Male), FEMALE; //member variable, private    PrivateString Info; //constructor, which is private    PrivateGender () {}PrivateGender (String info) { This. info =info; }    //member Methods     PublicString GetInfo () {returninfo; }}

An enumeration class can declare an abstract method, but there are specific enumeration values to implement.

 Public classDemo1 { Public Static voidMain (string[] args) {Gender male=Gender.male;        System.out.println (Male.getinfo ());    Male.speak (); }}enumGender {MALE (Male) {@Override Public voidspeak () {System.out.println ("It's a man."); }}, FEMALE {@Override Public voidspeak () {System.out.println ("It's a woman.");    }    }; //member Variables    PrivateString Info; //constructor Function    PrivateGender () {}PrivateGender (String info) { This. info =info; }    //member Methods     PublicString GetInfo () {returninfo; }     Public Abstract voidspeak ();}

An enumeration class can also implement an interface (serialization), or inherit an abstract class.

The Swith statement is extended in JDK5, which can receive an enumeration type (enum) in addition to receiving int, Byte, char, short, and so on.

 Public classDemo2 { Public Static voidMain (string[] args) {WeekDay Mon=Weekday.mon; Switch(Mon) { CaseMON:System.out.println ("Monday to work ...");  Break;  CaseTUE:System.out.println ("Tuesday, continue to work ...");  Break; }    }}enumWeekDay {MON, TUE, WED, THU, FRI, SAT, SUN;}

If the enumeration class has only one enumeration value, it can be used as a single-state design pattern.

Practice:

Please write an enumeration weekday on the day of the week, requirements: enumeration value: Mon,tue,wed,thu,fri,sat,sun The enumeration to have a method that calls the method to return the Chinese format of the week.

enumWeekDay {MON {@Override PublicString GetInfo () {return"Monday"; }}, TUE {@Override PublicString GetInfo () {return"Tuesday"; }}, WED {@Override PublicString GetInfo () {return"Wednesday"; }}, THU {@Override PublicString GetInfo () {return"Thursday"; }}, FRI {@Override PublicString GetInfo () {return"Friday"; }}, SAT {@Override PublicString GetInfo () {return"Saturday"; }}, SUN {@Override PublicString GetInfo () {return"Sunday";    }    };  Public AbstractString getInfo ();}

jdk1.5 new Features

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.