Java _ Qiao Xiaosong _ Java SE enhancement-static import _ automatic unpacking _ enhanced for loop _ Enumeration

Source: Internet
Author: User
Tags java se

 
JDK5.0 adds some features for simple development
1. Static Import
2. Automatic package installation/unpacking
3. For-each loop
4. variable parameters
5. Enumeration
6. Generic
7. Metadata
Using these features helps you write code that is clearer, lean, and secure.
 
Static Import
The static import syntax added by JDK 1.5 is used to import a static property value (method) or all static property values (methods) of a specified class)
Syntax:
Import static package name. Class Name. static attribute | static method | *
 Static import Statement use import static statement
Import a single static attribute of a specified class:
Import static java. lang. System. out
Imports static methods of a specified class
Import static java. lang. Math. max
Imports all static attributes and methods of a specified class.
Import static java. lang. Math .*
Automatic packing/unpacking
Autoboxing: assigns a basic data type to the corresponding packaging variable or the Object variable.
Automatic unpacking: The packaging class object is directly assigned to a corresponding basic type variable.
Enhance the for Loop
The reason for introducing an enhanced for loop: In versions earlier than JDK5, to traverse elements in an array or set, you must first obtain the array length or set iterator, Which is troublesome!
Therefore, JDK 5 defines a new syntax-enhance the for loop to simplify such operations. An enhanced for loop can only be used in an array or a collection class that implements the Iterator interface.
When you use foreach to traverse arrays and set elements cyclically, you do not need to obtain the array and set length, and you do not need to access array elements and set elements based on indexes. foreach loop automatically traverses each element of arrays and sets.
Syntax format:
For (type variable name: Set variable name ){... }
For (type varName: array | collection ){
// VarName automatically iterates to access each element
}
Package com. hbsi;


Import java. util. Iterator;
Import java. util. LinkedHashMap;
Import java. util. Map;
Import java. util. Set;


Public class Demo {


/**
* @ Param args
*/
Public static void main (String [] args ){
Demo d = new Demo ();
D. Test3 ();


}

/* Public void Test1 (){
Map map = new LinkedHashMap ();
Map. put (1, "aaa ");
Map. put (2, "bbb ");
Map. put (3, "ccc ");

Set set = map. entrySet ();
Iterator it = set. iterator ();
While (it. hasNext ()){
Map. Entry me = (Map. Entry) it. next ();
System. out. println (me. getKey () + "=" + me. getValue ());
}
}
Public void Test2 (){
Map map = new LinkedHashMap ();
Map. put (1, "aaa ");
Map. put (2, "bbb ");
Map. put (3, "ccc ");

Set set = map. entrySet ();
For (Object obj: map. entrySet ()){
Map. Entry me = (Map. Entry) obj;
System. out. println (me. getKey () + "=" + me. getValue ());
}
}*/
Public void Test3 (){
Map map = new LinkedHashMap ();
Map. put ("1", "aaa ");
Map. put ("2", "bbb ");
Map. put ("3", "ccc ");

Set set = map. keySet ();
Iterator it = set. iterator ();
While (it. hasNext ()){
String key = (String) it. next ();
String value = (String) map. get (key );
System. out. println (key + "=" + value );
}
}
}

Variable parameters
1. From JDK 1.5, Java allows you to define parameters with variable length, so that you can specify a number of uncertain parameters for the method.
2. When defining a method, add three points after the type of the last parameter (..., ... Is located between the variable type and the variable name, whether there are spaces before and after) indicates that the form parameter can accept multiple parameter values, multiple parameter values are passed as Arrays
3. The variable parameters can only be placed at the end of the parameter list. Therefore, a method can have only one variable-length parameter at most.
4. When you call a method that contains a variable parameter, You can input multiple parameters or an array for the parameter.
5. When a variable parameter method is called, the compiler implicitly creates an array for the variable parameter and accesses the variable parameter in an array form in the method body.
 
Enumeration class
 
Manual Implementation of enumeration classes:
Hide the constructor through private
Modify all possible instances of this class using public static final
Attributes cannot be modified, so private final should be used for modification.
 
Enumeration class
The above writing is quite troublesome. java provides a more convenient method-enumeration class can solve the above problems.
The enum keyword added in JDK 1.5 is used to define enumeration classes.
Enumeration classes are also special Java classes.
Differences between enumeration classes and common classes:
The enumeration class defined by enum inherits the java. lang. Enum class by default.
The constructor of the enumeration class can only use the private access controller.
All instances of the enumeration class must be explicitly listed (, separated; ended) in the enumeration class. The listed instance system will automatically add public static final Modification
JDK 5 extends the switch statement, which can receive int, byte, char, short, and an enumeration type.
If the enumeration has only one member, it can be implemented as a singleton mode.

Enumeration class attributes
Attributes of enumeration objects should not be modified. Therefore, privatefinal should be used for modification.
The privatefinal-modified attribute of the enumeration class should be assigned to it in the constructor.
If the enumeration class explicitly defines a constructor with parameters, the corresponding input parameters must also be used to list enumeration values.
 
Common enumeration methods
The enumerated classes declared in Java are subclasses of the java. lang. Enum class and inherit all methods of the Enum class. Common Methods:
Name (): returns the name of this enumeration constant and declares it in its enumeration declaration.
Ordinal (): returns the ordinal number of an enumerated constant (it is in the enumeration declaration position, where the ordinal number of the initial constant is zero ).
Valueof (Class enumClass, String name): return an enumeration constant of the specified enumeration type with the specified name. Static Method
Although this method cannot be found in the JDK documentation, each enumeration class has this method, which is very convenient to traverse all enumeration values of the enumeration class.

Example:
Package com. hbsi;


Public class Week {


/**
* @ Param args
*/
Public static void main (String [] args ){

System. out. println (WeekDay. Mon. getValue () + "-----" + WeekDay. Mon. toWeekDay ());


}


}
Enum WeekDay {

Mon ("Monday "){
@ Override
Public String toWeekDay (){

Return "Monday ";
}
},
Tue ("Tuesday "){
@ Override
Public String toWeekDay (){

Return "Tuesday ";
}
},
Wed ("Wednesday "){
@ Override
Public String toWeekDay (){

Return "Wednesday ";
}
},
The ("Thursday "){
@ Override
Public String toWeekDay (){

Return "Thursday ";
}
},
Fri ("Friday "){
@ Override
Public String toWeekDay (){

Return "Friday ";
}
},
Sat ("Saturday "){
@ Override
Public String toWeekDay (){

Return "Saturday ";
}
},
Sun ("Sunday "){
@ Override
Public String toWeekDay (){

Return "Sunday ";
}
};
Private WeekDay (String value ){
This. value = value;
}
Private String value;
Public String getValue (){
Return value;
}
Public abstract String toWeekDay ();
}

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.