Jdk1.7 New Features

Source: Internet
Author: User
Tags switch case

The following are some examples of new functions in Java 1.7:

1. Set support for set support for creating list/set/map is easier to write.

List string> List = [item];

String item = list [0];

Set string> set = {item };

Map string, integer> map = {key: 1 };

Int value = map [Key];

2. Automatic recovery management of resources automatic recovery management of resources the following code looks a bit troublesome... Isn't it?

Bufferedreader BR = new bufferedreader (New filereader (PATH ));

Try {return Br. Readline () ;}finally {Br. Close ();

I believe you will like the following method.

Try (bufferedreader BR = new bufferedreader (New filereader (PATH) {return Br. Readline ();

3. Simplified type references during the creation of generic instances simplified type references during instance Creation simplified

When the interface that declares your object is, you certainly do not want to specify the generic type again. Let's see how good this writing method is...

Mapstring, string> Hello = new map ();

1map Hello = new map ();

4. Use underscores in a number.

Int billion = 0000000_000_000; 1int billion = 0000000_000_000;

I don't know how useful this is.

5. Perform string-based operations on strings

Switch case string S = "test ";

Switch (s ){

Case "test": system. Out. println ("test ");

Case "test1": system. Out. println ("test1"); break;

Default: system. Out. println ("break"); break ;}

Note: before passing a string to the switch case, do not forget to check whether the string is null.

6. Binary symbols binary symbols you can add 0b before a binary character to create a binary type.

Int binary = 0b1001_1001;

7. capture multiple exception types in one catch

Try {Here comes your code ....}

Catch (ioexception | nullpointerexception | ..........){

8. Syntax support for set syntax ,,,, not necessarily an array, not an array, but an array.

Final list <integer> pidigits = [1, 2, 3, 4, 5, 8];

9. Some new tools for retrieving environment information and some new tools for retrieving Environment Information

File System. getjavaiotempdir () // Io Temporary Folder

File System. getjavahomedir () // JRE installation directory

File System. getuserhomedir () // current user directory

File System. getuserdir () // directory where the JAVA process is started

10. boolean type: Reverse type: null type: Secure

Boolean booleans. Negate (Boolean booleanobj) True => false, false => true, null => null

Boolean booleans. And (Boolean [] array)

Boolean booleans. Or (Boolean [] array)

Boolean booleans. XOR (Boolean [] array)

Boolean booleans. And (Boolean [] array)

Boolean booleans. Or (Boolean [] array)

Boolean booleans. XOR (Boolean [] array)

11. Safe addition, subtraction, multiplication, division, and security addition, subtraction, multiplication, division, and security

Int math. safetoint (long value)

Int math. safenegate (INT value)

Long math. safesubtract (long value1, int value2)

Long math. safesubtract (long value1, long value2)

Int math. safemultiply (INT value1, int value2)

Long math. safemultiply (long value1, int value2)

Long math. safemultiply (long value1, long value2)

Long math. safenegate (long value)

Int math. safeadd (INT value1, int value2)

Long math. safeadd (long value1, int value2)

Long math. safeadd (long value1, long value2)

Int math. safesubtract (INT value1, int value2)

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.