java new features

11
JAVA NEW FEATURES Vipul Agarwal

Upload: vipul-agarwal

Post on 18-Jan-2017

193 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Java New Features

JAVA NEW FEATURES Vipul

Agarwal

Page 2: Java New Features

UNDERSCORES IN NUMERIC LITERALSJava 7 introduced underscores in identifying the places. For example, you can declare 1000 as shown below:Valid:int thousand = 1_000;int million = 1_000_000;Float x = 3.14_15F;Long big = 9_2223_783_056_8766;Long hexBytes = 0xFF_EC_DE;

Invalid:float pi1 = 3_.1415F; float pi2 = 3._1415F;long ssn = 999_99_9999_L;int x1 = _52; int x1 = 52_;int x2 = 0_x52; int x2 = 0x_52;

Page 3: Java New Features

STRINGS IN SWITCH STATEMENTSBefore :A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types.

Now :it is supporting string data types also.int monthNameToDays(String s, int year) { switch(s) { case "April": case "June": case "September": case "November": return 30;

case "January": case "March": case "May": case "July": case "August": case "December": return 31;

case "February”: ... default:

Page 4: Java New Features

VARARGS WARNINGS – ERASURE import java.util.Arrays; import java.util.Date; import java.util.List;

class Test { @SuppressWarnings(value={"deprecation"}) public static void main(String[] args) { Date date = new Date(2008, 9, 30); System.out.println("date = " + date); } }

Page 5: Java New Features

MULTI-CATCH try { ... } catch(InstantiationException | NoSuchMethodException |

InvocationTargetException e) { // Useful if you do generic actions

log(e); }

Page 6: Java New Features

AUTOMATIC RESOURCE MANAGEMENT

Resources such as Connections, Files, Input/OutStreams, etc. should be closed by the developer by writing bog-standard code. Usually we use a try-finally block to close the respective resources.However, Java 7 has introduced another cool feature to manage the resources automatically. It is simple in operation, too. All we have to do is declare the resources in the try as follows:

java.lang.AutoCloseable More readable code and easy to write. Automatic resource management. Number of lines of code is reduced. No need of finally block just to close the resources.

Page 7: Java New Features

FOUR KEY NEW HELPER TYPES NEW IN JAVA 7 Class java.nio.file.Paths Class java.nio.file.Files Class java.nio.file.FileSystem Create Delete Copy Move/Rename Create Directories Attributes – Modified/Owner/Permissions/Size, etc.

Page 8: Java New Features

WATCHING A DIRECTORY Create a WatchService “watcher” for the

filesystem Register a directory with the watcher• Watcher” can be polled or waited on for

events• Events raised in the form of Keys• Retrieve the Key from the Watcher• Key has filename and events within it for

create/delete/modify

Page 9: Java New Features

WatchService watcher = FileSystems.getDefault().newWatchService();

Path dir = Paths.get("Path/To/Watched/Directory");

dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

Page 10: Java New Features

MAPPING JAVA.IO.FILE TO JAVA.NIO.FILE• java.io.File• File.canRead, canWrite,

canExecute• File.isDirectory(),

File.isFile(), and File.length()

• File.lastModified() and File.setLastModified(long)

• File methods: setExecutable, setReadable, setReadOnly, setWritable

• new File(parent, "newfile")

• java.nio.file.Path• Files.isReadable,

Files.isWritable, and Files.isExecutable.

• Files.isDirectory(Path, LinkOption...), Files.isRegularFile(Path, LinkOption...), and Files.size(Path)

• Files.getLastModifiedTime(Path, LinkOption...) and Files.setLastMOdifiedTime(Path, FileTime)

• Files methods: setAttribute(Path, String, Object, LinkOption...).

• parent.resolve("newfile")

Page 11: Java New Features

MAPPING JAVA.IO.FILE TO JAVA.NIO.FILE

• File.renameTo• File.delete• File.createNewFile• File.deleteOnExit• File.exists• File.compareTo and

equals• File.getAbsolutePath and

getAbsoluteFile• File.getCanonicalPath

and getCanonicalFile• File.isHidden• File.mkdir and mkdirs• File.listRoots

• Files.move• Files.delete• Files.createFile• DELETE_ON_CLOSE option in

createFile• Files.exists and Files.notExists• Path.compareTo and equals• Path.toAbsolutePath

• Path.toRealPath or normalize

• Files.isHidden• Path.createDirectory• FileSystem.getRootDirectories