Java 7 Language Enhancement

6,298 views

Published on

Covered topic in Java 7 Presentation (Project Coin):
• Java Language enhancement history from version JDK 1.1 to Java 7
• Definition of Java7 Project Coin from JSR 334 Lead Joseph D.Darcy’s
• Java 7 Language enhancement
1. String in Switch(alternative for if else statement with String)
2. Binary Literal(0B) and Underscore in literal for more readability (int binary=10_1)
3. Multi catch and more precious rethrow
4. Try with resource statement(Automatic resource management)
5. Diamond Operator(Generics)
6. Improved Compiler warning for varargs.

Published in: Technology, News & Politics
1 Comment
0 Likes
Statistics
Notes
  • Here are some usefull links with examples on java 7 language enhancements,
    http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-1.html
    http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-2.html
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
6,298
On SlideShare
0
From Embeds
0
Number of Embeds
3,251
Actions
Shares
0
Downloads
43
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Java 7 Language Enhancement

  1. 1.   Tossing the Project Coin!   Marimuthu Rajagopal
  2. 2. Java Version History Language Enhancement Version Release Date Language Enhancement JDK 1.0 Jan 23, 1996 (Initial Release)Oak JDK 1.1 Feb 19,1997 Inner Class ,Reflection,JavaBeans,JDBC,RMI J2SE 1.2 Dec 8, 1998 Strictfp, Collection frame work J2SE 1.3 May 8,2000 Hotspot JVM,JNDI,JPDA J2SE 1.4 Feb 6,2002 Assert, Regular expression,exception chaining. J2SE 1.5 Sep 30,2004 Generics,annotation,auto boxing,Enumeration,Var args,for each,staticimport Java SE 6 Dec 11,2006 Scripting Language Support,Performance Improvement Java SE 7 July 07,2011 <ul><ul><li>String in Switch </li></ul></ul><ul><ul><li>Binary literal & Underscore in literal </li></ul></ul><ul><ul><li>Multi-Catch and More precise rethrow. </li></ul></ul><ul><ul><li>Try-with-Resources statement(ARM). </li></ul></ul><ul><ul><li>Diamond Operator </li></ul></ul><ul><ul><li>Improved compiler warnings and Errors (Simplified varargs method invocation) </li></ul></ul>Java SE 8 <ul><ul><li>Lambda expression(Clousers) </li></ul></ul>
  3. 3. Java 7 Language Enhancement(Project Coin) JSR 334 <ul><li>Readability & Consistency </li></ul><ul><ul><li>String in Switch </li></ul></ul><ul><ul><li>Binary literal & Underscore in literal </li></ul></ul><ul><li>Very short error & resource handling </li></ul><ul><ul><li>Multi-Catch and More precise rethrow. </li></ul></ul><ul><ul><li>Try-with-Resources statement(ARM). </li></ul></ul><ul><li>Generics </li></ul><ul><ul><li>Diamond Operator </li></ul></ul><ul><ul><li>Improved compiler warnings for Varargs </li></ul></ul>
  4. 4. String in Switch <ul><ul><li>String object in the expression of a switch statement </li></ul></ul><ul><ul><li>Included in definition of the constant expression </li></ul></ul><ul><ul><li>Using hashcode and equals </li></ul></ul><ul><ul><li>NetBeans 7.0: Nested else if -> Switch Case. </li></ul></ul>http://download.java.net/jdk7/docs/technotes/guides/language/strings-switch.html http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.28 http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5313
  5. 5. String in Switch Java 6 and Prior public static String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay=&quot;&quot;; if(dayOfWeekArg.equals(&quot;Monday&quot;)) typeOfDay=&quot;Start of Work week&quot;; else if(dayOfWeekArg.equals(&quot;Tuesday&quot;)|| dayOfWeekArg.equals(&quot;Wednesday&quot;)|| dayOfWeekArg.equals(&quot;Thursday&quot;)) typeOfDay=&quot;Midweek&quot;; else if(dayOfWeekArg.equals(&quot;Friday&quot;)) typeOfDay=&quot;End of work week&quot;; else if(dayOfWeekArg.equals(&quot;Saturday&quot;)|| dayOfWeekArg.equals(&quot;Sunday&quot;)) typeOfDay=&quot;Week off&quot;; else typeOfDay=&quot;Invalid day&quot;; return typeOfDay; } http://download.java.net/jdk7/docs/technotes/guides/language/strings-switch.html
  6. 6. String in Switch Java 7 public static String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay=&quot;&quot;; switch (dayOfWeekArg) { case &quot;Monday&quot;: typeOfDay=&quot;Start of Work week&quot;; break; case &quot;Tuesday&quot;: case &quot;Wednesday&quot;: case &quot;Thursday&quot;: typeOfDay=&quot;Midweek&quot;; break; case &quot;Friday&quot;: typeOfDay=&quot;End of work week&quot;; break; case &quot;Saturday&quot;: case &quot;Sunday&quot;: typeOfDay=&quot;Week off&quot;; break; default: typeOfDay=&quot;Invalid day&quot;; break; } return typeOfDay; } http://download.java.net/jdk7/docs/technotes/guides/language/strings-switch.html
  7. 7. Binary literal & Underscore in literal <ul><ul><li>Improve readability </li></ul></ul><ul><ul><li>Existing literals (0-Oct,0x-Hex) ‏ </li></ul></ul><ul><ul><li>New Literal : (0b/0B-Binary all primitive data type and Wrapper class Byte,Short,Integer,Long ) ‏ </li></ul></ul><ul><ul><li>Underscore : </li></ul></ul><ul><ul><li>Long UID=1234_1234_1234; </li></ul></ul><ul><li>      int rs=10_000; </li></ul>http://download.java.net/jdk7/docs/technotes/guides/language/binary-literals.html http://download.java.net/jdk7/docs/technotes/guides/language/underscores-literals.html
  8. 8. Binary literal & Underscore in literal <ul><li>Binary Literal </li></ul><ul><li>Sample </li></ul><ul><ul><li>int binary= 0b 10000; int oct= 0 20; int hex= 0x 10; </li></ul></ul><ul><ul><li>output: 16 </li></ul></ul><ul><li>Underscore in literal: </li></ul><ul><ul><li>UID=1234_1234_1234; </li></ul></ul><ul><ul><li>place underscores only between digits. </li></ul></ul><ul><ul><li>Invalid Place </li></ul></ul><ul><ul><li>At the beginning or end of a number(int x1 = _52;int x3 = 52_;) ‏ </li></ul></ul><ul><ul><li>Adjacent to a decimal point in a floating point literal(float pi1 = 3_.1415F;float pi2 = 3._1415F;) ‏ </li></ul></ul><ul><ul><li>Prior to an F or L suffix(long socialSecurityNumber1 = 999_99_9999_L) ‏ </li></ul></ul><ul><ul><li>In positions where a string of digits is expected(int x5 = 0_x52;) ‏ </li></ul></ul><ul><ul><li>public class BinaryLiteral { </li></ul></ul><ul><ul><li>public static void main(String arg[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>int anInt2 = 0b10_1; </li></ul></ul><ul><ul><li>System.out.println(anInt2); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  9. 9. Multi-Catch and More precise rethrow. <ul><ul><li>Reduce code duplication </li></ul></ul><ul><ul><li>Single catch block handle more than one Exception (|) ‏ </li></ul></ul><ul><li>Try{} </li></ul><ul><li>catch(NullPointerException|ArrayIndexOutOfBoundsException exception ) </li></ul><ul><li>{} </li></ul><ul><ul><li>Catch parameter( exception) become implicitly final. </li></ul></ul><ul><ul><li>Generated Byte code size is less. </li></ul></ul>http://download.java.net/jdk7/docs/technotes/guides/language/catch-multiple.html
  10. 10. Multi-Catch. Java 6 and Prior try{ // Method method=Object.class.getMethod(&quot;toString&quot;); final Class[] ARG_TYPE=new Class[]{String.class}; Method method=Integer.class.getMethod(&quot;parseInt&quot;,ARG_TYPE); Object result=method.invoke(null,new Object[]{new String(&quot;44&quot;)}); System.out.println(&quot;result:&quot;+result.toString()); } catch(NoSuchMethodException e) { e.printStackTrace(); }catch(IllegalAccessException e) { e.printStackTrace(); } catch(InvocationTargetException e) { e.printStackTrace(); }
  11. 11. Multi-Catch. Java 7 try{ // Method method=Object.class.getMethod(&quot;toString&quot;); final Class[] ARG_TYPE=new Class[]{String.class}; Method method=Integer.class.getMethod(&quot;parseInt&quot;,ARG_TYPE); Object result=method.invoke(null,new Object[]{new String(&quot;44&quot;)}); System.out.println(&quot;result:&quot;+result.toString()); } catch( NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); }
  12. 12. More precise rethrow. Java 6 and Prior public static void precise() throws NoSuchMethodException, IllegalAccessException { try{ // Method method=Object.class.getMethod(&quot;toString&quot;); final Class[] ARG_TYPE=new Class[]{String.class}; Method method=Integer.class.getMethod(&quot;parseInt&quot;,ARG_TYPE); Object result=method.invoke(null,new Object[]{new String(&quot;44&quot;)}); System.out.println(&quot;result:&quot;+result.toString()); } catch(Exception e) { throw new NoSuchMethodException(); } }
  13. 13. More precise rethrow. Java 7 public static void precise() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { try{ // Method method=Object.class.getMethod(&quot;toString&quot;); final Class[] ARG_TYPE=new Class[]{String.class}; Method method=Integer.class.getMethod(&quot;parseInt&quot;,ARG_TYPE); Object result=method.invoke(null,new Object[]{new String(&quot;44&quot;)}); System.out.println(&quot;result:&quot;+result.toString()); } catch(ReflectiveOperationException e) { throw e; } }
  14. 14. Try-with-Resources statement. <ul><ul><li>Automatic Resource Management(ARM) ‏ </li></ul></ul><ul><ul><li>Try statement declares one or more resources(object) ‏ </li></ul></ul><ul><ul><li>Declared resource will be closed end of the statement. </li></ul></ul><ul><ul><li>Any object implements AutoCloseable eligible for resource. </li></ul></ul><ul><li>Syntax: try ResourceSpecification Block Catchesopt Finallyopt </li></ul>http://download.java.net/jdk7/docs/technotes/guides/language/catch-multiple.html
  15. 15. Try-with-Resources statement. Java 6 and Prior static void jdk16writeFile(String path) { BufferedWriter bw = null; try { bw=new BufferedWriter(new FileWriter(path)); bw.write(&quot;JDK1.6 Need to close resource manualy.&quot;); }catch(IOException e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
  16. 16. Try-with-Resources statement. Java 7 static void jdk17writeFile(String path) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) { bw.write(&quot;Welcome to JDK1.7 Try with resource concept&quot;); } catch(IOException ex) ‏ { System.out.println(&quot;ex&quot;+ex); } }
  17. 17. Diamond Operator(Type Inference for Generic Instance Creation) ‏ <ul><ul><li>Replace type Argument with empty set of Parameter(<>) ‏ </li></ul></ul><ul><ul><li>Jdk1.6 List<String> userList=new ArrayList<String>(); </li></ul></ul><ul><ul><li>Jdk1.7 List<String> userList=new ArrayList<>(); </li></ul></ul><ul><ul><li>userList.addAll(new ArrayList<>());//Failed </li></ul></ul>http://download.java.net/jdk7/docs/technotes/guides/language/catch-multiple.html
  18. 18. Diamond Operator(Type Inference for Generic Instance Creation) ‏ In Java 6 and Prior List<String> jvmLanguages=new ArrayList<String>(); In Java 7 List<String> jvmLanguages=new ArrayList<>(); jvmLanguages.add(&quot;Groovy&quot;); jvmLanguages.add(&quot;Scala&quot;); System.out.println(jvmLanguages); Type Inference List<?> typeinference=new ArrayList<> ();
  19. 19. Improved compiler warnings for Varargs <ul><ul><li>Uses: </li></ul></ul><ul><ul><li>@SafeVarargs </li></ul></ul><ul><ul><li>@SuppressWarning({“unchecked”,”varargs”); does not suppress warning generated from method call. </li></ul></ul><ul><li><T> List<T> Arrays.asList<T… a> </li></ul><ul><li><T> boolean Collection.addAll(Collection<? Super T> c,T… elements) </li></ul>
  20. 20. Thank you Q&A    e-mail :muthu.svm@gmail.com blog :http://microtechinfo.blogspot.com

×