java 7 language enhancement

20
Tossing the Project Coin! Marimuthu Rajagopal

Upload: muthusvm

Post on 26-May-2015

5.973 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Java 7 Language Enhancement

 

Tossing the Project Coin! 

Marimuthu Rajagopal

Page 2: Java 7 Language Enhancement

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 ImprovementJava SE 7 July 07,2011 • String in Switch

• Binary literal & Underscore in literal• Multi-Catch and More precise rethrow.• Try-with-Resources statement(ARM).• Diamond Operator• Improved compiler warnings and Errors

(Simplified varargs method invocation)

Java SE 8 • Lambda expression(Clousers)

Page 3: Java 7 Language Enhancement

Java 7 Language Enhancement(Project Coin)JSR 334

Readability & Consistency • String in Switch• Binary literal & Underscore in literal

Very short error & resource handling• Multi-Catch and More precise rethrow.• Try-with-Resources statement(ARM).

Generics• Diamond Operator• Improved compiler warnings for Varargs

Page 4: Java 7 Language Enhancement

String in Switch

• String object in the expression of a switch statement

• Included in definition of the constant expression

• Using hashcode and equals

• NetBeans 7.0: Nested else if -> Switch Case.

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

Page 5: Java 7 Language Enhancement

String in Switch

Java 6 and Priorpublic static String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay=""; if(dayOfWeekArg.equals("Monday")) typeOfDay="Start of Work week"; else if(dayOfWeekArg.equals("Tuesday")|| dayOfWeekArg.equals("Wednesday")|| dayOfWeekArg.equals("Thursday")) typeOfDay="Midweek"; else if(dayOfWeekArg.equals("Friday")) typeOfDay="End of work week"; else if(dayOfWeekArg.equals("Saturday")|| dayOfWeekArg.equals("Sunday")) typeOfDay="Week off"; else typeOfDay="Invalid day"; return typeOfDay; }

http://download.java.net/jdk7/docs/technotes/guides/language/strings-switch.html

Page 6: Java 7 Language Enhancement

String in Switch

Java 7public static String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay=""; switch (dayOfWeekArg) { case "Monday": typeOfDay="Start of Work week"; break; case "Tuesday": case "Wednesday": case "Thursday": typeOfDay="Midweek"; break; case "Friday": typeOfDay="End of work week"; break; case "Saturday": case "Sunday": typeOfDay="Week off"; break; default: typeOfDay="Invalid day"; break; } return typeOfDay; }

http://download.java.net/jdk7/docs/technotes/guides/language/strings-switch.html

Page 7: Java 7 Language Enhancement

Binary literal & Underscore in literal

• Improve readability• Existing literals (0-Oct,0x-Hex)• New Literal: (0b/0B-Binary all primitive

data type and Wrapper class Byte,Short,Integer,Long )

• Underscore: Long UID=1234_1234_1234;

      int rs=10_000;

http://download.java.net/jdk7/docs/technotes/guides/language/binary-literals.htmlhttp://download.java.net/jdk7/docs/technotes/guides/language/underscores-literals.html

Page 8: Java 7 Language Enhancement

Binary literal & Underscore in literal

Binary LiteralSample

int binary=0b10000; int oct=020; int hex=0x10; output: 16

Underscore in literal: UID=1234_1234_1234; place underscores only between digits. Invalid Place• At the beginning or end of a number(int x1 = _52;int x3 = 52_;)• Adjacent to a decimal point in a floating point literal(float pi1 = 3_.1415F;float pi2 = 3._1415F;)• Prior to an F or L suffix(long socialSecurityNumber1 = 999_99_9999_L)• In positions where a string of digits is expected(int x5 = 0_x52;)

public class BinaryLiteral {

public static void main(String arg[]){

int anInt2 = 0b10_1; System.out.println(anInt2);

}

}

Page 9: Java 7 Language Enhancement

Multi-Catch and More precise rethrow.

• Reduce code duplication• Single catch block handle more than one

Exception(|)

Try{}catch(NullPointerException|ArrayIndexOutOfBoundsException exception){}• Catch parameter(exception) become

implicitly final.• Generated Byte code size is less.

http://download.java.net/jdk7/docs/technotes/guides/language/catch-multiple.html

Page 10: Java 7 Language Enhancement

Multi-Catch.

Java 6 and Prior try{ // Method method=Object.class.getMethod("toString"); final Class[] ARG_TYPE=new Class[]{String.class}; Method method=Integer.class.getMethod("parseInt",ARG_TYPE); Object result=method.invoke(null,new Object[]{new String("44")}); System.out.println("result:"+result.toString()); }catch(NoSuchMethodException e) { e.printStackTrace(); }catch(IllegalAccessException e) { e.printStackTrace(); } catch(InvocationTargetException e) { e.printStackTrace(); }

Page 11: Java 7 Language Enhancement

Multi-Catch.

Java 7

try{// Method method=Object.class.getMethod("toString");final Class[] ARG_TYPE=new Class[]{String.class};Method method=Integer.class.getMethod("parseInt",ARG_TYPE);Object result=method.invoke(null,new Object[]{new String("44")});System.out.println("result:"+result.toString());}catch( NoSuchMethodException | IllegalAccessException | InvocationTargetException e){e.printStackTrace();}

Page 12: Java 7 Language Enhancement

More precise rethrow.

Java 6 and Prior

public static void precise() throws NoSuchMethodException, IllegalAccessException { try{ // Method method=Object.class.getMethod("toString"); final Class[] ARG_TYPE=new Class[]{String.class}; Method method=Integer.class.getMethod("parseInt",ARG_TYPE); Object result=method.invoke(null,new Object[]{new String("44")}); System.out.println("result:"+result.toString()); }catch(Exception e) { throw new NoSuchMethodException(); } }

Page 13: Java 7 Language Enhancement

More precise rethrow.

Java 7

public static void precise() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{try{// Method method=Object.class.getMethod("toString");final Class[] ARG_TYPE=new Class[]{String.class};Method method=Integer.class.getMethod("parseInt",ARG_TYPE);Object result=method.invoke(null,new Object[]{new String("44")});System.out.println("result:"+result.toString());}catch(ReflectiveOperationException e){throw e;}}

Page 14: Java 7 Language Enhancement

Try-with-Resources statement.

• Automatic Resource Management(ARM)• Try statement declares one or more

resources(object)• Declared resource will be closed end of

the statement.• Any object implements AutoCloseable

eligible for resource.

Syntax: try ResourceSpecification Block Catchesopt Finallyopt

http://download.java.net/jdk7/docs/technotes/guides/language/catch-multiple.html

Page 15: Java 7 Language Enhancement

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("JDK1.6 Need to close resource manualy."); }catch(IOException e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException ex) { ex.printStackTrace(); } } }

Page 16: Java 7 Language Enhancement

Try-with-Resources statement.

Java 7

static void jdk17writeFile(String path) {try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {bw.write("Welcome to JDK1.7 Try with resource concept");}catch(IOException ex){System.out.println("ex"+ex);}}

Page 17: Java 7 Language Enhancement

Diamond Operator(Type Inference for Generic Instance Creation)

• Replace type Argument with empty set of Parameter(<>)

• Jdk1.6 List<String> userList=new ArrayList<String>();

• Jdk1.7 List<String> userList=new ArrayList<>();

• userList.addAll(new ArrayList<>());//Failed

http://download.java.net/jdk7/docs/technotes/guides/language/catch-multiple.html

Page 18: Java 7 Language Enhancement

Diamond Operator(Type Inference for Generic Instance Creation)

In Java 6 and PriorList<String> jvmLanguages=new ArrayList<String>();

In Java 7List<String> jvmLanguages=new ArrayList<>();jvmLanguages.add("Groovy");jvmLanguages.add("Scala");System.out.println(jvmLanguages);Type InferenceList<?> typeinference=new ArrayList<> ();

Page 19: Java 7 Language Enhancement

Improved compiler warnings for Varargs

Uses:@SafeVarargs@SuppressWarning({“unchecked”,”varargs”); does not suppress

warning generated from method call.

<T> List<T> Arrays.asList<T… a><T> boolean Collection.addAll(Collection<? Super T> c,T… elements)

Page 20: Java 7 Language Enhancement

Thank you

Q&A  

e-mail:[email protected] blog:http://microtechinfo.blogspot.com