Consider the following program and choose the appropriate option:
import java.util.*; public class Main { public static void main(String []args) { Map<String, int> map = new HashMap<int, String>(); //#1 Map<String, String> map2 = new HashMap<String, String>(); //#2 Map<String, String> map3 = new HashMap<>(); //#3 Map<> map4 = new HashMap<String, String>(); //#4 }/* w w w . j av a 2 s . com*/ }
b)
Due to the diamond syntax, it is optional to specify template types in the right hand side of an object creation statement.
Hence, statement #3 is right.
Statement #2 is correct since HashMap is a Map.
Therefore, option b) is correct.
In statement #1, the order of arguments of the declared type is different from the order of arguments in the initialized type.
In statement #4, the diamond syntax is used in the declaration of the type and so is incorrect (the correct way is to use the diamond operator in the initialization type).