InterviewSolution
| 1. |
Java Generics |
|
Answer» Generics refer to types that have been parameterized. The goal is to make type (Integer, String, etc., as well as user-defined types) a parameter for methods, classes, and interfaces. Generics can be used to design classes that function with a variety of data types. A generic entity is a type that works on a parameterized type, such as a class, interface, or method. In Java, generics are equivalent to templates in C++. Generics are used extensively in classes such as HashSet, ArrayList, HashMap, and others. Generic class: In the same way that C++ specifies parameter types, we use <> to define parameter types in generic class formation. The following syntax is used to construct generic class objects. ClassName <Type> obj = new ClassName <Type>()Here, ‘ClassName’ denotes the name of the class whose instance is to be created. <Type> specifies the data type to be used while instantiating the object. ‘obj’ is the name of the object to be created. Example - class Sample<T>{ // Declaring an object of type T T obj; Test(T obj) // constructor { this.obj = obj; } public T getObject() { return this.obj; } } class Test { public static void main (String[] args) { // instance of Integer type Sample <Integer> obj1 = new Sample<Integer>(10); System.out.println(obj1.getObject()); // instance of String type Test <String> obj2 = new Test<String>("Interview Bit"); System.out.println(obj2.getObject()); } } Output - 10Interview Bit In the above example, a generic class Sample has been created which accepts a parameter T to determine the type of the class. We create 2 instances of the Sample class. One with an Integer type and the other with a String type. The following are the advantages of using generics in Java: Type-safety: In generics, we can only hold one type of object. It does not allow for the storage of objects of different types. Example - List list = new ArrayList();list.add(1); list.add("Interview Bit"); The above code snippet runs fine. This is because we have not specified any type while creating an instance of the List class. However, if we run the below code snippet, we get a compile-time error. List<Integer> list = new ArrayList<Integer>();list.add(1); list.add("Interview Bit");// compile-time error This is because we have specified the instance of the List class to be of type Integer. Typecasting isn’t necessary: When we use generics, we do not need to typecast the object when we access it. Example - List list = new ArrayList();list.add("Interview Bit"); String s = (String) list.get(0);//typecasting In the above code, we can see that before accessing the element of the list, we need to typecast it to String type explicitly. This is because we have not declared the list instance to be of a specific type. However, if we run the below code snippet, we do not need to typecast it explicitly. List<String> list = new ArrayList<String>();list.add("Interview bit"); String s = list.get(0); This is because we have specified the instance of the List class to be of String type. Checking at Compile Time: It is checked at compile time to ensure that an issue does not arise at runtime. It is considerably better to handle the problem at compile time than at runtime, according to an excellent programming approach. Example - List<String> list = new ArrayList<String>();list.add("Interview Bit"); list.add(100);//Compile Time Error The above code snippet gives a compile-time error. This verifies that the code is checked at compile-time. |
|