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

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
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
 
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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

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")