7 new features of Java 7

Source: Internet
Author: User

1. Language support for collection classes;

2. Automatic resource management;

3. Improved generic instance creation type inference;

4. Digital literal underline support;

Use string in 5.switch;

6. Binary literals;

7. Simplify variable parameter method invocation.

====================== Gorgeous split-line ======================

1. Language support for collection classes

Java will contain first-class language support for the creation of collection classes. This means that the collection class can be created like Ruby and Perl.

What was needed:

1          list<string> List = new arraylist<string> ();
2 List.add ("item");
3 String item = list.get (0);
4
5 set<string> Set = new hashset<string> ();
6 Set.add ("item");
7 map<string, integer> Map = new hashmap<string, integer> ();
8 map.put ("key", 1);
9

Now it's just this: (These collections are immutable ...) )

1          list<string> List = ["Item"];
2 String item = list[0];
3
4 set<string> Set = {"Item"};
5
6 map<string, integer> Map = {"Key": 1};
7

====================== Gorgeous split-line ======================

2. Automatic resource Management

Some resources in Java need to be closed manually, such as Inputstream,writes,sockets,sql classes. This new language feature allows the try statement itself to request additional resources that act on the try block and automatically close.

The previous wording:

1          bufferedreader br = new BufferedReader (new FileReader (path));
2 try {
3 return Br.readline ();
4 } finally {
5 br.close ();
6

Now you can: (a bit like C #)

1          Try (bufferedreader br = new BufferedReader (new FileReader (path)) {
2 return Br.readline ();
3

====================== Gorgeous split-line ======================

3. Improved generic instance creation type inference;

Type inference is a special annoyance, such as the following code:

By type inference becomes:

1 map<string, list<string>> anagrams = new hashmap<> ();

Note: This <> is called the diamond operator, and after Java 7 This operator infers the type from the referenced declaration.

====================== Gorgeous split-line ======================

4. Digital literal underline support

Long numbers are not readable, and in Java 7 You can use underscores to separate the long int from the longer. Such as:

int one_million = 1_000_000;

It's a shame to be like this ... But the readability is good indeed.

====================== Gorgeous split-line ======================

Use string in 5.switch

This is one of the reasons I dislike switch in Java, where I used to use only number or enum in switch. Now you can use a string, Haha, good, praise!

1          String s = ...
2 switch (s) {
3 Case "Quux":
4 Processquux (s);
5 //Fall-through
6 Case "foo":
7 Case "Bar":
8 Processfooorbar (s);
9 Break ;
Ten case "Baz":
Processbaz (s);
/ /Fall-through
Default:
Processdefault (s);
Break ;
16

====================== Gorgeous split-line ======================

6. Binary literals

Because of the C language, Java code traditionally forces programmers to use only decimal, octal, or hexadecimal notation (numbers).

Because few domains are bit-oriented, this limitation can lead to errors. You can now create binary literals using the 0b prefix:

Now you can use this representation of binary literals, and with very short code, you can convert binary characters to data types, such as in byte or shorter.

1 byte abyte = (byte) 0b001;    
2 Short ashort = (short) 0b010;

====================== Gorgeous split-line ======================

7. Simplify variable parameter method invocation.

When a programmer tries to use a mutable parameter that is not materialized and calls a *varargs* (mutable) method, the editor generates a warning that is not safe to operate.
JDK 7 moves the warning from call to the method declaration (Methord declaration) in the process. This allows the API designer to use VARARG because the number of warnings is greatly reduced.

7 new features of Java 7

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.