Saturday, 9 April 2016

Diamond Syntax in JAVA

"Diamond Syntax" is the project coin improvement in JAVA 7.
For example, Earlier we use to declare collections like this:

ArrayList<String> str=new ArrayList<String>();

       Map<String,File> myfile=new HashMap<String,File>();

Notice that the type parameters are duplicated in these declarations. As of Java 7 these declarations could be simplified to:

ArrayList<String> str=new ArrayList<>();

       Map<String,Filemyfile=new HashMap<>();

This <> is known as Diamond operator.

NOTE: You cannot swap these , the following is NOT legal.

          Map<> myfile=new HashMap<String,File>();  // Not a legal Diamond Syntax



No comments:

Post a Comment