List<Object> myList = new ArrayList<String>(); //(hint: no)
Map<Integer> myMap = new HashMap<int>(); // (hint: also no)
Why are the statements wrong in the above declarations?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s look at the first example. Think of the operations you should be able to do on a
List<Object>: Add, Remove, Retrieve any Object.You’re trying to fill those requirements with a collection that you can Add, Remove, and Retrieve only strings.
The second is simply because Generics in Java don’t support primitive types. For more information, you could check out:
java – Why don’t Generics support primitive types?