Project Coin

Preview:

Citation preview

 

Tossing the Project Coin! 

Marimuthu Rajagopal

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)

Java 7 Language Enhancement(Project Coin)JSR 334

“Project Coin is a suite of language and library Changes to make things programmers do every 

day easier”.

Coin, n. A piece of small changeCoin, v. To create new language

Joseph D.Darcy’s –Lead(http://blogs.oracle.com/darcy )

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

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

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

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

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

• 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

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_;)A• Adjacent to a decimal point in a floating point literal(float pi1 = 3_.1415F;float pi2 = 3._1415F;)A• Prior to an F or L suffix(long socialSecurityNumber1 = 999_99_9999_L)P• In positions where a string of digits is expected(int x5 = 0_x52;)I

public class BinaryLiteral {

public static void main(String arg[]){

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

}

}

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

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(); }

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();}

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(); }            }

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;}}

Try-with-Resources statement.

• Automatic Resource Management(ARM)A• Try statement declares one or more resources(object)r

• 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

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(); }  }  }

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)c{System.out.println("ex"+ex);}}

Diamond Operator(Type Inference for Generic Instance Creation)I

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

• 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

Diamond Operator(Type Inference for Generic Instance Creation)I

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<> ();

Improved compiler warnings for Varargs

• Summary :no longer receive uninformative unchecked compiler warning from calling platform library methods.

 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)

Thank you

Q&A  

e-mail:muthu.svm@gmail.com blog:http://microtechinfo.blogspot.com 

Recommended