SlideShare a Scribd company logo
1 of 11
Just to drink a cup of coffee
refence guide: from java 6 to java 7 (SE)
Syntax
Compiler infers the generic type parameters
Improving variables declarations
Numbers litterals
Improving numebr reading (really?)
String switch
Exception
Catch more exceptions
Avoiding code redundancy
Try with resources
Avoiding “if/try/catch/finally nightmare”
Multithread
Fork and Join Framework
Parallel computing
NIO
deleteIfExists, …
...and a lot of stuff
File Watcher
Java Typed
java.lang.invoke
Not typed language are easy
Compiler infer the generic type parameters
Java 6
bject> objects = new LinkedList<MyObject>();
Java 7
List<MyObject> objects = new Lin
Numbers
Java 6
int i = 1000;
int j =1000000
Java 7
int i = 1_000;
int j = 1_000_000;
Wrong syntax
int x = _1_000_000;
String switch
Java 6
String a,b,c;
if (a.equals(b)) {
} else if (a.equals(c)) {
}
Java 7
String a,b,c;
switch (a) {
b:
break;
c:
break;
}
Catch more exceptions
Java 6
try {
...
} catch(NumberFormatException e) {
logError(e);
} catch(NullPointerException e) {
logError(e);
}
Java 7
try {
...
} catch(NumberFormatException |
logError(e);
}
Try with resources
(implements Closeable or AutoCloseable)
Java 6
FileReader r = null;
try{
r = new FileReader(file);
BufferedReader reader = new BufferedReader(r);
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
} catch(IOException e) {
logError(e);
} finally {
if (r!=null) r.close();
}
Java 7
try(BufferedReader reader = new BufferedReader(
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
}
Join Fork Framework
more powerfull than the code described here [http://www.oracle.com/technetwork/articles/java/fork-join-422606.ht
Java 6
FutureTask<String> myFutureAction = new FutureTask<String>(
new Callable<String>()
{
public MyObject call() {
Return ...;
}
});
ExecutorService executor = Executors.newFixedThreadPool(1);
FutureTask<Object> futureOne = new FutureTask<Object>(myFutureAction);
executor.execute(future);
…//sleep
executor.shutdown();
Java 7
public class ForkAction extends RecursiveAct
ParamObject from = null;
ForkAction(ParamObject from, ParamObject to)
this.from=from;
...
}
@Override
public MyObject compute() {
...
}
}
...
ForkJoinPool forkJoinPool = new ForkJoinPool
forkJoinPool.invoke(new ForkAction(from, to)
NIO deleteIfExists, ...
Java 6
try {
Files.delete(path);
} catch(NoSuchFileException e){}
Java 7
Files.deleteIfExists(path);
and a lot of stuff
Files.createSymbolicLink(...)
Files.copy(...)
Files.move(...)
NIO WatcherService
Java 6
while (true) {
File myDir = …
pollForChanges(myDir);
}
public void pollForChanges(File myDir) {
//list dir
//use apache common io
}
Java 7
Path pathToWatch = Paths.get("/tmp");
PathToWatch.register(FileSystems.getDefault(
while (true) {
WatchKey key = watchService.take();
}
NIO WatcherService
Java 6
while (true) {
File myDir = …
pollForChanges(myDir);
}
public void pollForChanges(File myDir) {
//list dir
//use apache common io
}
Java 7
Path pathToWatch = Paths.get("/tmp");
PathToWatch.register(FileSystems.getDefault(
while (true) {
WatchKey key = watchService.take();
}
java.lang.invoke
Java 6
class MyClass {
public void myMethod(int i, String j) {
...
}
}
...
Class<?>[] argTypes = new Class[] { int.class, String.class };
meth = MyClass.class. getDeclaredMethod("myMethod", argTypes);
meth.invoke(new MyClass(), 2, "EFG");
Java 7
class MyClass {
public void myMethod(int i, String j) {
...
}
}
...
MethodHandle mh;
MethodType desc = MethodType.methodType(v
mh = MethodHandles.lookup().findVirtual(M
mh.invokeExact(new MyClass(), 1, "ABCDE")

More Related Content

What's hot

Java 7 & 8
Java 7 & 8Java 7 & 8
Java 7 & 8
Ken Coenen
 

What's hot (20)

Core Java - Quiz Questions - Bug Hunt
Core Java - Quiz Questions - Bug HuntCore Java - Quiz Questions - Bug Hunt
Core Java - Quiz Questions - Bug Hunt
 
JVM
JVMJVM
JVM
 
Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Java 7 & 8
Java 7 & 8Java 7 & 8
Java 7 & 8
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
Java concurrency questions and answers
Java concurrency questions and answers Java concurrency questions and answers
Java concurrency questions and answers
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
Java7
Java7Java7
Java7
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modules
 
Monitoring distributed (micro-)services
Monitoring distributed (micro-)servicesMonitoring distributed (micro-)services
Monitoring distributed (micro-)services
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
 
Java after 8
Java after 8Java after 8
Java after 8
 
DevNexus 2020: Discover Modern Java
DevNexus 2020: Discover Modern JavaDevNexus 2020: Discover Modern Java
DevNexus 2020: Discover Modern Java
 
Porting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQLPorting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQL
 
50 new features of Java EE 7 in 50 minutes
50 new features of Java EE 7 in 50 minutes50 new features of Java EE 7 in 50 minutes
50 new features of Java EE 7 in 50 minutes
 

Similar to From Java 6 to Java 7 reference

Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
elliando dias
 

Similar to From Java 6 to Java 7 reference (20)

Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
 
Java 7 & 8 New Features
Java 7 & 8 New FeaturesJava 7 & 8 New Features
Java 7 & 8 New Features
 
Nantes Jug - Java 7
Nantes Jug - Java 7Nantes Jug - Java 7
Nantes Jug - Java 7
 
55j7
55j755j7
55j7
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Java
JavaJava
Java
 
New Features Of JDK 7
New Features Of JDK 7New Features Of JDK 7
New Features Of JDK 7
 
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new features
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the question
 
JavaFXScript
JavaFXScriptJavaFXScript
JavaFXScript
 
What`s new in Java 7
What`s new in Java 7What`s new in Java 7
What`s new in Java 7
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptJava_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 

More from Giacomo Veneri

Eracle project poster_session_efns
Eracle project poster_session_efnsEracle project poster_session_efns
Eracle project poster_session_efns
Giacomo Veneri
 

More from Giacomo Veneri (17)

Giacomo Veneri Thesis 1999 University of Siena
Giacomo Veneri Thesis 1999 University of SienaGiacomo Veneri Thesis 1999 University of Siena
Giacomo Veneri Thesis 1999 University of Siena
 
Giiacomo Veneri PHD Dissertation
Giiacomo Veneri PHD Dissertation Giiacomo Veneri PHD Dissertation
Giiacomo Veneri PHD Dissertation
 
Industrial IoT - build your industry 4.0 @techitaly
Industrial IoT - build your industry 4.0 @techitalyIndustrial IoT - build your industry 4.0 @techitaly
Industrial IoT - build your industry 4.0 @techitaly
 
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
 
Pattern recognition on human vision
Pattern recognition on human visionPattern recognition on human vision
Pattern recognition on human vision
 
Advanced java
Advanced javaAdvanced java
Advanced java
 
Bayesain Hypothesis of Selective Attention - Raw 2011 poster
Bayesain Hypothesis of Selective Attention - Raw 2011 posterBayesain Hypothesis of Selective Attention - Raw 2011 poster
Bayesain Hypothesis of Selective Attention - Raw 2011 poster
 
Raw 2009 -THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH A MODEL TO E...
Raw 2009 -THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH  A MODEL TO E...Raw 2009 -THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH  A MODEL TO E...
Raw 2009 -THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH A MODEL TO E...
 
Eracle project poster_session_efns
Eracle project poster_session_efnsEracle project poster_session_efns
Eracle project poster_session_efns
 
Dal web 2 al web 3
Dal web 2 al web 3Dal web 2 al web 3
Dal web 2 al web 3
 
Il web 2.0
Il web 2.0Il web 2.0
Il web 2.0
 
Cibernetica, modelli computazionali e tecnologie informatiche
Cibernetica, modelli computazionali e tecnologie  informatiche Cibernetica, modelli computazionali e tecnologie  informatiche
Cibernetica, modelli computazionali e tecnologie informatiche
 
Giacomo Veneri Thesis
Giacomo Veneri ThesisGiacomo Veneri Thesis
Giacomo Veneri Thesis
 
EVA – EYE TRACKING - STIMULUS INTEGRATED SEMI AUTOMATIC CASE BASE SYSTEM
EVA – EYE TRACKING - STIMULUS INTEGRATED SEMI AUTOMATIC CASE  BASE SYSTEMEVA – EYE TRACKING - STIMULUS INTEGRATED SEMI AUTOMATIC CASE  BASE SYSTEM
EVA – EYE TRACKING - STIMULUS INTEGRATED SEMI AUTOMATIC CASE BASE SYSTEM
 
THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH
THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH
THE ROLE OF LATEST FIXATIONS ON ONGOING VISUAL SEARCH
 
Evaluating Human Visual Search Performance by Monte Carlo methods and Heurist...
Evaluating Human Visual Search Performance by Monte Carlo methods and Heurist...Evaluating Human Visual Search Performance by Monte Carlo methods and Heurist...
Evaluating Human Visual Search Performance by Monte Carlo methods and Heurist...
 
Giacomo Veneri 2012 phd dissertation
Giacomo Veneri 2012 phd dissertationGiacomo Veneri 2012 phd dissertation
Giacomo Veneri 2012 phd dissertation
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 

From Java 6 to Java 7 reference

  • 1. Just to drink a cup of coffee refence guide: from java 6 to java 7 (SE) Syntax Compiler infers the generic type parameters Improving variables declarations Numbers litterals Improving numebr reading (really?) String switch Exception Catch more exceptions Avoiding code redundancy Try with resources Avoiding “if/try/catch/finally nightmare” Multithread Fork and Join Framework Parallel computing NIO deleteIfExists, … ...and a lot of stuff File Watcher Java Typed java.lang.invoke Not typed language are easy
  • 2. Compiler infer the generic type parameters Java 6 bject> objects = new LinkedList<MyObject>(); Java 7 List<MyObject> objects = new Lin
  • 3. Numbers Java 6 int i = 1000; int j =1000000 Java 7 int i = 1_000; int j = 1_000_000; Wrong syntax int x = _1_000_000;
  • 4. String switch Java 6 String a,b,c; if (a.equals(b)) { } else if (a.equals(c)) { } Java 7 String a,b,c; switch (a) { b: break; c: break; }
  • 5. Catch more exceptions Java 6 try { ... } catch(NumberFormatException e) { logError(e); } catch(NullPointerException e) { logError(e); } Java 7 try { ... } catch(NumberFormatException | logError(e); }
  • 6. Try with resources (implements Closeable or AutoCloseable) Java 6 FileReader r = null; try{ r = new FileReader(file); BufferedReader reader = new BufferedReader(r); String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } } catch(IOException e) { logError(e); } finally { if (r!=null) r.close(); } Java 7 try(BufferedReader reader = new BufferedReader( String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } }
  • 7. Join Fork Framework more powerfull than the code described here [http://www.oracle.com/technetwork/articles/java/fork-join-422606.ht Java 6 FutureTask<String> myFutureAction = new FutureTask<String>( new Callable<String>() { public MyObject call() { Return ...; } }); ExecutorService executor = Executors.newFixedThreadPool(1); FutureTask<Object> futureOne = new FutureTask<Object>(myFutureAction); executor.execute(future); …//sleep executor.shutdown(); Java 7 public class ForkAction extends RecursiveAct ParamObject from = null; ForkAction(ParamObject from, ParamObject to) this.from=from; ... } @Override public MyObject compute() { ... } } ... ForkJoinPool forkJoinPool = new ForkJoinPool forkJoinPool.invoke(new ForkAction(from, to)
  • 8. NIO deleteIfExists, ... Java 6 try { Files.delete(path); } catch(NoSuchFileException e){} Java 7 Files.deleteIfExists(path); and a lot of stuff Files.createSymbolicLink(...) Files.copy(...) Files.move(...)
  • 9. NIO WatcherService Java 6 while (true) { File myDir = … pollForChanges(myDir); } public void pollForChanges(File myDir) { //list dir //use apache common io } Java 7 Path pathToWatch = Paths.get("/tmp"); PathToWatch.register(FileSystems.getDefault( while (true) { WatchKey key = watchService.take(); }
  • 10. NIO WatcherService Java 6 while (true) { File myDir = … pollForChanges(myDir); } public void pollForChanges(File myDir) { //list dir //use apache common io } Java 7 Path pathToWatch = Paths.get("/tmp"); PathToWatch.register(FileSystems.getDefault( while (true) { WatchKey key = watchService.take(); }
  • 11. java.lang.invoke Java 6 class MyClass { public void myMethod(int i, String j) { ... } } ... Class<?>[] argTypes = new Class[] { int.class, String.class }; meth = MyClass.class. getDeclaredMethod("myMethod", argTypes); meth.invoke(new MyClass(), 2, "EFG"); Java 7 class MyClass { public void myMethod(int i, String j) { ... } } ... MethodHandle mh; MethodType desc = MethodType.methodType(v mh = MethodHandles.lookup().findVirtual(M mh.invokeExact(new MyClass(), 1, "ABCDE")