Sunday, December 8, 2019

What is the correct and incorrect piece of code for generic representation in Java? [Onward Java 5]

Problem statement: We need to find out the correct pieces of java code.
  1. ArrayList<Integer> l = new ArrayList<>();
  2. ArrayList<Integer> l = new ArrayList<Integer>();
  3. ArrayList<int> l = new ArrayList<int>();
  4. ArrayList<int> l = new ArrayList<>();
  5. LinkedList<Integer> l = new LinkedList<Integer>();
  6. LinkedList<Integer> l = new LinkedList<>();
  7. LinkedList<int> l = new LinkedList<>();
  8. Map<String, Integer> m = new HashMap<String, Integer>();
  9. Map<String, Integer> m = new HashMap<>();
  10. Map<String, int> m = new HashMap<>();
Answer:
  1. Correct- we can keep right angular braces empty
  2. Correct- we can specify the right angular braces data type also
  3. Incorrect-generic support only wrapper class for auto boxing to Object type
  4. Incorrect-generic support only wrapper class for auto boxing to Object type
  5. Correct- we can specify the right angular braces data type also
  6. Correct- we can keep right angular braces empty
  7. Incorrect- generic doen't support primitive data type
  8. Correct - we can specify the right angular braces data type also
  9. Correct - we can keep right angular braces empty
  10. Incorrect- generic doen't support primitive data type

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...