Prev Next

Java / Generics

1. What are Generic Types?

Generic types refers to generic class, abstract class or interface that is parameterized.

Read full answer

2. How can you suppress unchecked warning in Java ?

using @SuppressWarnings("unchecked") annotation. @SuppressWarnings ( "unchecked" ) List < String > myList = new ArrayList();

Read full answer

3. How do you create a generic method which accepts generic types and return a generic type?

Replace all the raw type with the generic types like E,K, V etc and also add the list of generic types before the return type using <>. public V StoreThePair(K key, V value) { return V; }

Read full answer

4. Example of a Bounded generic method.

static T processTheNumber(T number){ T result = number; return result; }

Read full answer

5. Explain Generics using a sample code.

List list = new ArrayList(); list.add("No Generics is used "); String s = (String) list.get(0); In the above example, ArrayList can hold any type of objects, however we need to cast each object to its appropriate data type since get method's return type is Object. A string literal is added to the...

Read full answer

6. Generics.

Generics introduced in Java 1.5, facilitates strong-typing on the collection objects. Generics defines the object types that the collection can hold. It provides compile time type-safety and ensures that only the particular type is added in collection and eliminates ClassCastException in runtime.

Read full answer

7. What are Generic Methods?

Generic method are methods that is type parameterized. public class GenericMethodExample { public static void main (String [] s) { String [] StrArray = new String [] { "Find" , "the" , "word" , "in" , "this" , "Array" }; System.out.println(contains(StrArray, "word" )); Integer [] intArray = new I...

Read full answer

8. Advantages of using Generics.

Helps creating type-safe collections objects. Facilitate Strong type validation during the compile-time. Eliminates Cast operator. mproves the performance as Generics eliminates casting, boxing/unboxing. Enables creation of generic algorithms, customizing the algorithm to be compatible with colle...

Read full answer

9. How do you define a generic type?

The Generic types are defined as <"GenericType"> e.g. to differentiate it from T if is a actual type.

Read full answer

10. Why Generics is required?

Generics helps finding issues during compile time rather than runtime. Generics are parameter for types.

Read full answer

11. What is type erasure?

Type erasure refers to the compile-time process where compiler erases type information during compile time and no type is available at runtime.

Read full answer

12. Example of a generic method from JAVA Collections API.

Collections.sort() that sorts collection of elements or objects of any type is a generic method.

Read full answer

13. What is Bounded wild-cards in Generics?

Bounded Wildcards impose bound on Type. There are two types of Bounded wild-cards. Bounded wildcards are specified by List Bounded wildcards which impose an upper bound by ensuring that type must be sub class of T. impo...

Read full answer

14. Can we use Generics with Array?

Arrays does not support generics.

Read full answer

15. Can you pass List<Number> to a method which accepts List<Object>?

No. We encounter compilation error as it is incompatible.

Read full answer

16. Difference between List<Object> and List<?>.

You may add a new Objects or its sub-type into List whereas you could only insert null into the List

Read full answer

17. Unbounded Wildcards.

Also known as Collection of Unknown type, specified using the wildcard character(?). for e.g., List , Collection . An example of unbounded wildcards. public static void printCollection (List myList) { for (Object key: myList) { System.out.print(key + " " ); } }

Read full answer

18. When do you implement Unbounded wildcards?

It is applicable when your method does not to have to do anything with the individual element types, for e.g. it can be used to determine the length of the list of any type.

Read full answer

19. What is SuppressWarnings ("unchecked") in Java?

The SuppressWarning annotation is used to suppress compiler warning for the annotated element. Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts.

Read full answer

20. What is raw type in Java?

Raw type refers to generic type without specifying any parametrized type. For example, List is a raw type while List is a parameterized type. Raw types are retained for the backward compatibility to support code developed in older Java versions.

Read full answer

«
»

Comments & Discussions