2.Using Strings in Switch Statements
3.Automatic resource management
4.Numeric literals with underscores
5.Improved exception handling
6.File change Notifications
7.Fork and Join
8.Supporting dynamism
9.JLayerPane
10.Binary Literals
List<String> al=new ArrayList<String>();
List<String> al=new ArrayList<>();
3. Automatic resource management
import java.io.*;
public class FileTest {
public static void main(String[] args) {
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fos = new FileOutputStream("file.txt");
dos = new DataOutputStream(fos);
dos.writeUTF("test");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
dos.close();
} catch (IOException e) {
}
}
}
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Java7FileTest {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("file.txt");
DataOutputStream dos = new DataOutputStream(fos)) {
dos.writeUTF("test");
} catch (IOException e) {
}
}
}
4. Numeric literals with underscores
But there are some rules for adding underscores between digits
1.Underscore must be in between digits not at the beginning or at the end.
2.If the numeric literal in floating point type then underscore must not be adjacent to a decimal point.
3. Underscore must not prior to F and L suffix and in the occurrence of string of digit where it is expected
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Java7File1Test {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("file.txt");
DataOutputStream dos = new DataOutputStream(fos)) {
dos.writeUTF("test");
SimpleDateFormat ff = new SimpleDateFormat("MM-dd-yyyy");
ff.parse("2012-12-12");
} catch (IOException | ParseException e) {
}
}
}
6. File change Notifications
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class DirectoryWatcher {
public static void main(String[] args) {
// Obtain a path reference to your watchable directory:
Path tmpPath = Paths.get("D:/Pretech Files");
WatchService watchService;
try {
watchService = FileSystems.getDefault().newWatchService();
// Register the directory with the WatchService for all types of
// events
tmpPath.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE);
// Initiate the infinite loop and start taking the events:
for (;;) {
WatchKey key = watchService.take();
// Run through the events on the key:
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
switch (kind.name()) {
case "ENTRY_CREATE":
System.out.println("Directory created: " + event.context());
break;
case "ENTRY_MODIFY":
System.out.println("Directory Modified: " + event.context());
break;
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
*
* This class helps to understand the feature of ForkJoinPool and ForkJoinTasks
*
* 1) ForkJoinTask is an abstract class ,RecursiveAction and RecursiveTasks are
* the subclasses of ForkJoinTask class
*
* 2) In case of return type required we need to use RecursiveTask
*
* 3) In this example we used RecursiveAction and override its call() method to
* implement fork and join
*
* 4) This is an example to print 1 to the given number
*
* 5) The given number is less then 100 it will not split the task (fork)
*
* 6) If the given number is 100 or greater than 100 it will split it in to two
* task (1 to 50 and 51 to 100) 7) Finally join each task
*
* @authorvinod
*/
public class ForkAndJoinExample {
public static void main(String[] args) {
ForkJoinPool fjpool = new ForkJoinPool(2);
RecursiveAction task = new PrintJob(100);
fjpool.invoke(task);
}
}
class PrintJob extends RecursiveAction {
private static final long serialVersionUID = 1L;
private int lines = 0;
public PrintJob(int lines) {
this.lines = lines;
}
@Override
protected void compute() {
if (lines < 100) {
print(1, lines, "single task");
} else {
PrintJob p1 = new PrintJob(lines);
p1.print(1, lines / 2, "task 1");
PrintJob p2 = new PrintJob(lines);
p2.print(lines / 2, lines, "task 2");
p1.fork();
p2.fork();
p1.join();
p2.join();
}
}
private void print(int start, int end, String taskname) {
for (int i = start; i <= end; i++) {
System.out.println("print job triggerd from " + taskname + " " + i);
}
}
}
8. Supporting dynamism
9. JLayerPane
Java has introduced another great feature for swing UI developer which is a JLayeredPane. With use of JLayeredPane we can draw on the top of the component and handle the events without actually modifying the underlying component. This is a great feature because it can be used in many effects such as transition effects, blur effects or spotlight effects etc.
10. Binary Literals
https://github.com/kkvinodkumaran/java7.git
References
1. http://code.joejag.com/2009/new-language-features-in-java-7/2. http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.html
3. http://inebium.com/post/java-7-new-release-performance-code