jdk1.7 new Features

Source: Internet
Author: User

Recently studied the new features added in jdk1.7, the first need to change the JDK version, configuration environment variables, so after the toss, Java-version found that is jdk1.7 version, but I use Eclipse development, but always appear syntax compilation error. Odd a strange, whether to replace the compile-time with the JDK or the new project when the choice of jdk1.7 are not, and finally re-download 64-bit eclipse, then naturally OK, it seems to be the problem of Eclipse. Here are the new features in jdk1.7:

First: Numbers allow underscores, and note that you can only place underscores between numbers

/** * Numbers allow underlining, it is important to note that only the underscore is placed between the numbers */int number = 100_00_00; SYSTEM.OUT.PRINTLN (number); Long Creditcardnumber = 1234_5678_9012_3456l;long SocialSecurityNumber = 999_99_9999l; float pi = 3.14_15f;long hexbytes = 0xff_ec_de_5e;long Hexwords = 0xcafe_babe;long Maxlong = 0x7fff_ffff_ffff_ffffl;byte n Ybbles = 0b0010_0101;long bytes = 0b11010010_01101001_10010100_10010010;
Second: Direct support: Binary, octal, hexadecimal representation
/** * binary, octal, hexadecimal means */int Binarynumber = 0b011;//here represents 2 binary System.out.println (binarynumber); int eightnumber = 022;// This represents 8 binary, converted to decimal is 18system.out.println (eightnumber), int sixteennumber = 0x14;//here represents 16 binary, converted to decimal is 20system.out.println ( Sixteennumber);
Third: threadlocalrandom concurrent generation of random numbers, thread safety

Threadlocalrandom localrandom = Threadlocalrandom.current ();  Localrandom.nextdouble ();
IV: The switch statement allows the use of strings

The/*** switch statement allows the use of the string */string name = "Xiaoming"; switch (name) {<span style= "white-space:pre" ></span>case " Xiaohong ":     <span style=" White-space:pre "></span>system.out.println (" Your name is: "+name); <span Style= "White-space:pre" ></span>break;case "xiaoming": System.out.println ("Your name is:" +name); Default:break;}
V: Generic instantiation type automatic inference

/*** generic instantiation Type automatic inference * map<string, list<string>> myMap = new hashmap<string, list<string>> (); */List <string>list = new arraylist<> (); Map<string,list<string>>map = new hashmap<> ();
Sixth: JDK7 allows multiple exceptions to be captured, note that the parameter ex in Cath is the final type

/** * JDK7 allows multiple exceptions to be caught, note that the parameter ex in Cath is the final type of */file file = new file ("/local/jdk7/"); try {Reader reader = new Inputstreamread ER (new FileInputStream (file)); Reader.read ();} catch (NullPointerException | IOException ex) {ex.printstacktrace ();}

Sixth: Try-with-resources, can automatically close the related resources

/** * Try-with-resources, can automatically close the related resources * (this resource implements the Autocloseable interface, JDK7 for most of the resource objects have implemented this interface) * @throws FileNotFoundException  */public string ReadLine (String filePath) throws IOException {try (BufferedReader reader = new BufferedReader (New Fil EReader (New File (FilePath))) {return reader.readline ();}}
Seventh: Watchservice monitoring file system changes, here I wrote a method, the incoming disk under a path, you can listen to the path of the file changes, delete and so on.

/** * Watchservice Monitor file system changes */public static void Testwatchservice (Path watchpath) {try {watchservice watchservice = Filesy Stems.getdefault (). Newwatchservice (); Watchpath.register (Watchservice,standardwatcheventkinds.entry_create, Standardwatcheventkinds.entry_delete,standardwatcheventkinds.entry_modify); while (true) {  Watchkey key = Watchservice.take ();  For (watchevent<?> event:key.pollEvents ()) {  Watchevent.kind Kind = Event.kind ();  if (kind = = Standardwatcheventkinds.overflow) {//event may lost or discarded  continue;  }  Watchevent<path> e = (watchevent<path>) event;  Path fileName = E.context ();  System.out.printf ("Event%s has Happened,which fileName is%s%n"  , Kind.name (), filename);  }  if (!key.reset ()) {break  ;  }  }  } catch (IOException | Interruptedexception e) {e.printstacktrace ();}}
Then this is called in the Main method:

Testwatchservice (Paths.get ("D:", "haha"));

Here I am listening to the changes in the contents of this folder, such as: D:/haha

Eighth: Filevisitor Recursive Call folder:

/** * Recursive call folder */public static void Diguidirectory (Path directpath) {try {files.walkfiletree (directpath,new filevisitor& Lt Path> () {public void Find (path path) {System.out.printf ("Access-%s:%s%n", (Files.isdirectory (Path)? ") Directory ":" File "), Path.getfilename ());} @Overridepublic filevisitresult previsitdirectory (Path dir,<span style= "White-space:pre" ></span> Basicfileattributes attrs) throws IOException {//TODO auto-generated method Stubreturn filevisitresult.continue;} @Overridepublic filevisitresult visitfile (Path file,basicfileattributes attrs) throws IOException {//TODO Auto-generated method Stubfind (file); return filevisitresult.continue;} @Overridepublic filevisitresult visitfilefailed (Path file,ioexception exc) throws IOException {//TODO auto-generated  Method StubSystem.out.println ("Error:" +exc); return filevisitresult.continue;} @Overridepublic filevisitresult postvisitdirectory (Path dir,ioexception exc) throws IOException {//TODO auto-generated Method Stubfind (dir); return FilevIsitresult.continue;}});} catch (IOException e) {e.printstacktrace ();}}
Here I query the file under the newly created haha folder diguidirectory (Paths.get ("D:", "haha");
At this point, the console prints the following statement:

Access-File: Test.txt
Access-Directory: SDF
Access-File: Test.txt
Access-Directory: haha


Also on the Internet to see such as these new features, these features, I can not tell whether it is correct, anyway, in my jdk1.7 version of the test is not established. I do not know if anyone and I encounter the same problem, and finally how to refuse.

New tool methods for environment information

File System.getjavaiotempdir ()//IO Temp folder

File System.getjavahomedir ()//JRE installation directory

File System.getuserhomedir ()//Current User Directory

File System.getuserdir ()//directory where the Java process was started 5

Boolean type reversal, NULL pointer safe, participating bit operation

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)

Safe subtraction.
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)

A collection is syntactically supported, not necessarily an array

Java code:

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













jdk1.7 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.