Java enhanced syntax

Source: Internet
Author: User
Tags iterable

Add the syntax after JAVA1.5:

 

 

[Java]
1. Generic programming
C uses the template technology to specify the element type of the Set, and Java has never had the corresponding function before 1.5. An object of any type can be put in a set. When we take objects from the set, we also have to forcibly convert them. Tigers introduced generics. Example:
ArrayList <String> list = new ArrayList <String> ();
List. add (new String ("Hello! "));
System. out. println (list. get (0 ));
2. Automatic packing/unpacking
In Java, conversion between the original data types and their packaging classes is often required, especially when the data is put into the Collection container and taken out. To reduce the code duplication, JDK 5.0 adds the automatic packing/unboxing operation. After that, the original data type can be basically operated as an object.
Automatic packing: A developer can assign a basic data type to the corresponding packaging class.
Automatic unpacking: A developer can assign a packaging object to the corresponding basic data type.
Example: public void test (){
Integer I = 1000; // boxed
Int c = I; // unpack
// Typical application cases
List list = new ArrayList ();
List. add (12); // boxed
Int temp = (Integer) list. get (0); // unpack
}
3. Enhanced loop structure (for-each 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! To simplify code development, especially for the Collection container traversal, Iterator is required in the past, and next () is called to get the next object and hasNext () is called () to determine whether there are still such repeated code, it is easy to use foreach. Therefore, JDK 5 defines a new syntax-Enhancing the foreach loop to simplify such operations. The enhanced for loop can only be used in arrays or collection classes that implement the Iterable interface. Syntax format:
For (variable type variable: array or set to be iterated) {Specific Code}
Example: int data [] = {1, 2, 3, 4, 5 };
For (int j: data)
System. out. println (j );
4. Enumeration type
Java adds support for enumeration types and defines them using the enum keyword. The following is an example:
Public enum Meal {BREAKFAST, LUNCH, SUPPER };
Public static void main (String [] args ){
Meal m = Meal. BREAKFAST;
System. out. println (m );
}
Every constant defined in this statement is an instance of Meal. You can see that the definition of Enumeration type is very similar to a class. You can have methods and constructor, but the constructor cannot be public or protected. In addition, the instance must be defined at the beginning. In addition, enum cannot be inherited, because after an enum is defined, the compiler automatically generates. lang. the class with the same name of Enum, while Java does not support multiple inheritance, so the defined enum cannot be inherited, which sometimes brings some restrictions, for example, if you want to reuse code to expand enum. In addition, note that, in the syntax defined above, instances are separated by commas (,), and all instances are ended with semicolons.
The enumeration type also defines some methods, such as toString (), values (), and ordinal.
Enum also supports static import and can be used in switch statements.
5. Static Import
Static import is very simple, that is, to import static variables or methods of a class, so that you do not need to write the Class Name When referencing, reducing the workload of code input. Example:
Import static java. lang. System. out;
Public class StaticImport {
Public static void main (String [] args ){
Out. println ("Good ");
}
}
The most common output statement is used in the Code. Originally, System. out. println () needs to be written. Now, to import the out statement statically, you only need to write out. println ()
Generally, the imported statement can also be written as import static java. lang. System. * to import all static variables and methods in this class. As you can see, static import is very simple. Remember that although this feature is generally called "static import", it must be written as "import static" when writing code ".
6. Variable Parameter List
The functions referenced in C can be used if the method wants to accept any number of parameters. The obtained parameters are read one by one using the foreach syntax. In the past, a lot of overload methods may be required. The format is as follows:
Void argtest (Object... args ){
For (int I = 0; I <args. length; I ++)
System. out. println (I );
}
Example:
Public class Test {
Public static void test (int... values ){
System. out. println (values. length );
For (int I: values ){
System. out. println (I );
}
}
Public static void main (String [] args ){
Test (1 );
Test (1, 2 );
Test (1, 2, 3 );
}
} As you can see in the code above, the variable parameter list also has a property of length.
Then we can call this function as follows:
Argtest (1 );
Argtest (1, 2, 4, 5, 6 );

7. format the output
System. out. printf ("This is a test: % 4.2f \ n", 123.123 );
This will print out: This is a test: 123.12
8. read data from the terminal
When I first came into contact with Java, I could not use Java to read data as conveniently as C in the terminal. I used to get an integer from the terminal as follows:
Try {
BufferedReader reader = new BufferedReader (new
InputStreamReader (System. in ));
Int I = Integer. parseInt (reader. readLine ());
} Catch (IOException e ){}
Now, Java introduces an iterator (), which can easily read data from the terminal:
Public class ScannerExample {
Public static void main (String [] args ){
Partition read = new partition (System. in );
Int I = read. nextInt ();
System. out. println ("input I =" + I );
}
}

1. Generic programming
C uses the template technology to specify the element type of the Set, and Java has never had the corresponding function before 1.5. An object of any type can be put in a set. When we take objects from the set, we also have to forcibly convert them. Tigers introduced generics. Example:
ArrayList <String> list = new ArrayList <String> ();
List. add (new String ("Hello! "));
System. out. println (list. get (0 ));
2. Automatic packing/unpacking
In Java, conversion between the original data types and their packaging classes is often required, especially when the data is put into the Collection container and taken out. To reduce the code duplication, JDK 5.0 adds the automatic packing/unboxing operation. After that, the original data type can be basically operated as an object.
Automatic packing: A developer can assign a basic data type to the corresponding packaging class.
Automatic unpacking: A developer can assign a packaging object to the corresponding basic data type.
Example: public void test (){
Integer I = 1000; // boxed
Int c = I; // unpack
// Typical application cases
List list = new ArrayList ();
List. add (12); // boxed
Int temp = (Integer) list. get (0); // unpack
}
3. Enhanced loop structure (for-each 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! To simplify code development, especially for the Collection container traversal, Iterator is required in the past, and next () is called to get the next object and hasNext () is called () to determine whether there are still such repeated code, it is easy to use foreach. Therefore, JDK 5 defines a new syntax-Enhancing the foreach loop to simplify such operations. The enhanced for loop can only be used in arrays or collection classes that implement the Iterable interface. Syntax format:
For (variable type variable: array or set to be iterated) {Specific Code}
Example: int data [] = {1, 2, 3, 4, 5 };
For (int j: data)
System. out. println (j );
4. Enumeration type
Java adds support for enumeration types and defines them using the enum keyword. The following is an example:
Public enum Meal {BREAKFAST, LUNCH, SUPPER };
Public static void main (String [] args ){
Meal m = Meal. BREAKFAST;
System. out. println (m );
}
Every constant defined in this statement is an instance of Meal. You can see that the definition of Enumeration type is very similar to a class. You can have methods and constructor, but the constructor cannot be public or protected. In addition, the instance must be defined at the beginning. In addition, enum cannot be inherited, because after an enum is defined, the compiler automatically generates. lang. the class with the same name of Enum, while Java does not support multiple inheritance, so the defined enum cannot be inherited, which sometimes brings some restrictions, for example, if you want to reuse code to expand enum. In addition, note that, in the syntax defined above, instances are separated by commas (,), and all instances are ended with semicolons.
The enumeration type also defines some methods, such as toString (), values (), and ordinal.
Enum also supports static import and can be used in switch statements.
5. Static Import
Static import is very simple, that is, to import static variables or methods of a class, so that you do not need to write the Class Name When referencing, reducing the workload of code input. Example:
Import static java. lang. System. out;
Public class StaticImport {
Public static void main (String [] args ){
Out. println ("Good ");
}
}
The most common output statement is used in the Code. Originally, System. out. println () needs to be written. Now, to import the out statement statically, you only need to write out. println ()
Generally, the imported statement can also be written as import static java. lang. System. * to import all static variables and methods in this class. As you can see, static import is very simple. Remember that although this feature is generally called "static import", it must be written as "import static" when writing code ".
6. Variable Parameter List
The functions referenced in C can be used if the method wants to accept any number of parameters. The obtained parameters are read one by one using the foreach syntax. In the past, a lot of overload methods may be required. The format is as follows:
Void argtest (Object... args ){
For (int I = 0; I <args. length; I ++)
System. out. println (I );
}
Example:
Public class Test {
Public static void test (int... values ){
System. out. println (values. length );
For (int I: values ){
System. out. println (I );
}
}
Public static void main (String [] args ){
Test (1 );
Test (1, 2 );
Test (1, 2, 3 );
}
} As you can see in the code above, the variable parameter list also has a property of length.
Then we can call this function as follows:
Argtest (1 );
Argtest (1, 2, 4, 5, 6 );
 
7. format the output
System. out. printf ("This is a test: % 4.2f \ n", 123.123 );
This will print out: This is a test: 123.12
8. read data from the terminal
When I first came into contact with Java, I could not use Java to read data as conveniently as C in the terminal. I used to get an integer from the terminal as follows:
Try {
BufferedReader reader = new BufferedReader (new
InputStreamReader (System. in ));
Int I = Integer. parseInt (reader. readLine ());
} Catch (IOException e ){}
Now, Java introduces an iterator (), which can easily read data from the terminal:
Public class ScannerExample {
Public static void main (String [] args ){
Partition read = new partition (System. in );
Int I = read. nextInt ();
System. out. println ("input I =" + I );
}
}

 

 


Add the syntax after Java 1.7:


[Java]
: Improved integer support.
 
Int I = 0b0011;
Int j = 0b0011_0101;
 
Binary Code starting with 0B or 0b can assign values to integer types, and supports the following _-separated binary integer types, so that the code can be read and written better.
 
2: swicth is an integer of String.
Copy code
 
Switch (s ){
Case "1 ":
Break;
Case "2 ":
Break;
Case "3 ":
Break;
 
}
 
Copy code
 
3: extended support for generics
 
Map <String, String> map = new HashMap <> ();
 
In fact, this function does not bring too much change, especially Java still does not change the false generic type.
 
4: Multi-Level catch
 
Try {
}
Catch (SQLException | IOException e ){
E. printStackTrace ();
}
 
5. Automatic Resource Management
 
When the Closeable class is inherited, the system automatically closes the resource when the method exits.
 
6: FileSystem API support
 
Java 7 supports a wide range of file systems, including copy, move, and delete operations, file system monitoring, and recursion.

1: Improved integer support.

Int I = 0b0011;
Int j = 0b0011_0101;

Binary Code starting with 0B or 0b can assign values to integer types, and supports the following _-separated binary integer types, so that the code can be read and written better.

2: swicth is an integer of String.
Copy code

Switch (s ){
Case "1 ":
Break;
Case "2 ":
Break;
Case "3 ":
Break;

}

Copy code

3: extended support for generics

Map <String, String> map = new HashMap <> ();

In fact, this function does not bring too much change, especially Java still does not change the false generic type.

4: Multi-Level catch

Try {
}
Catch (SQLException | IOException e ){
E. printStackTrace ();
}

5. Automatic Resource Management

When the Closeable class is inherited, the system automatically closes the resource when the method exits.

6: FileSystem API support

Java 7 supports a wide range of file systems, including copy, move, and delete operations, file system monitoring, and recursion.

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.